Log in

View Full Version : Explode Function Help



DeSaaD37
03-05-2009, 07:42 PM
Just wondering if any of you can help me out with a small issue I've been having with PHP.

I've got a Database setup on this web site I'm currently working on FundraiserFactory.com. If you check the front page, you will see that under "Fund Raising Products and Ideas" There's a list of keywords that I need alphabetized.

Here's how the Database is Setup with 2 examples:


Products Table:
p_id: 10
p_name: Clothing and Apparel
p_keywords: T-Shirt, Canvas Bag, Tote Bag, drawstring, Backpack
p_folder: clothing_and_apparel

p_id: 11
p_name: Food and Beverage
p_keywords: Gourmet Food, Cookie, Pizza, Dessert, Cheesecake
p_folder: food_and_beverage


The problem is that I need to be able to take all those keywords, break them up, and alphabetize them on my front page at FundraiserFactory.com. Here's what I tried so far:



$query = "SELECT * FROM products ORDER BY p_keywords ASC";
$result = mysql_query($query);

for ($count=0; $count < mysql_numrows($result); $count++) {

$keywords = mysql_result($result, $count, "p_keywords");
$product_link = mysql_result($result, $count, "p_folder");

$keyword_array = explode(', ', $keywords);
sort($keyword_array);

foreach ($keyword_array as $key) {
print "<a href=\"http://fundraiserfactory.com/products/$product_link\">$key</a>, ";
}
}



All this is doing though, is taking each set of keyword arrays and alphabetizing them instead of breaking up the words and making them individuals.

I would like to just have all the keywords in each array, put into one Array instead of multiple arrays. (And then Sort that Array once all the product keywords are listed together.)

This is the first time I've ever had to use the Explode function, so any help is greatly appreciated!

JasonDFR
03-06-2009, 09:29 AM
This one is a bit complicated. If I didn't comment the code well enough, let me know what questions you have.

I'd appreciate any feedback, especially if someone else has a better way to do this.

Good luck,

J


<?php

// Get the $result from your db query
// $rows is example data I used
$rows =
array(
array(
'p_folder' => 'clothing_and_apparel',
'p_keywords' => 'T-Shirt, Canvas Bag, Tote Bag, drawstring, Backpack'
),
array(
'p_folder' => 'food_and_beverage',
'p_keywords' => 'Gourmet Food, Cookie, Pizza, Dessert, Cheesecake'
)
);

// Create an array to hold the information used to later build the links
$links = array();

// for loop to go through each row pulled from your db
// replace count with mysql_num_rows like in your example
for ($i = 0; $i < count($rows); $i++) {

// Make keywords string into an array
$keywords = explode(', ', $rows[$i]['p_keywords']);

// Use keyword array and your product folder to make the links array
// Each key of the links array will be a keyword
foreach ( $keywords as $keyword ) {

$keyword = ucfirst($keyword);
$links[$keyword] = $rows[$i]['p_folder'];

}

}

// Sort the links array alphabetically by key k(ey)sort()
ksort($links);

$count = count($links);
$i = 1;

// echo out your links
// the $count keeps a comma from showing up after the last link
foreach ( $links as $key => $value ) {

echo '<a href="http://fundraiserfactory.com/products/' . $value . '">' . $key . '</a>' . (($i < $count) ? ', ' : '');
$i++;

}

?>

DeSaaD37
03-13-2009, 07:18 PM
That works perfectly. It's exactly what I needed. The only problem I'm having now is getting the Array's to loop correctly from the Database.

Here's the code I'm using to access the DB:



$result = mysql_query("SELECT * FROM products ORDER BY p_keywords ASC");

while ($rows = mysql_fetch_assoc($result)) {
if (!empty($rows['p_keywords'])){

$keywords = $rows['p_keywords'];
$folder = $rows['p_folder'];
}
}


I've already tried to add "$rows" into the loop, but it's not spitting out the way you had it. I also tried these in the loop:



$array= array(
'p_folder' => $folder,
'p_keywords' => $keywords
);


$rows = array($array);


and



$rows =
array(
array(
'p_folder' => $keywords,
'p_keywords' => $folder
)
);

I understand that I need the ARRAY inside the ARRAY to loop, I just don't know how to do that. I really do appreciate all your help. I'm so close to finishing this site, this is the only thing really holding me back since I'm spending so much time trying to figure it out.

CrazyChop
03-15-2009, 03:33 PM
You need to store each keyword and folder as you take them out from the DB.



$result = mysql_query("SELECT * FROM products ORDER BY p_keywords ASC");

$data = array();

while ($rows = mysql_fetch_assoc($result)) {
if (!empty($rows['p_keywords'])){

$keywords = $rows['p_keywords'];
$folder = $rows['p_folder'];

array_push($data, array( "keyword" => $keywords, "folder" => $folder));
}
}

echo "<pre>".print_r($data, true)."</pre>";



array_push will append the keyword and folder as an associative array into the $data array, creating an array inside an array.

Jason's $rows example is a sample DB data return, btw.

Your code on the first post looks like it would work. Have you tried a print_r() on the $keyword_array or print out the $keyword string and ensure that the delimiter is comma or that the output is something as what you have expected?

On Edit: Oh I got it. You have to store the keywords in an array first, then sort them all together to get the desired result. The explode() part is working fine.

DeSaaD37
03-16-2009, 04:59 PM
Thank you guys so much for your help. You are a life saver!