|
creating category structure php mysql
Hi all,
I've spent the past couple of days trying to sort out the category structure for a new web site i'm building. First of all i was informed it was going to be just 2 levels deep, now its looking as though it could be 3 or 4 levels deep for some cats.
The part i'm really struglling with is getting the 3 level deep cats to show up properly in a drop down menu, as i need it to form part of an add product form.
I can do the 2 levels, and that works, and i've tried to create a work around to get the three levels working. So far if i get the three levels showing up in the dd menu, i only get those showing up and no other cats, with a mysql error message for each three level cats that are showing up, calling up an mysql error in the query, (hope you follow me).
I've also tried running the code from a category tree table, and the normal cats table, but just can't quite fix it.
I've included the code below, its kind of a mess as i've spent most of the past 3 hours trying to fix it and ended up confusing myself, so if anybody can point me in the right direction or even pass on advice as to the best way of doing this bit i'd be most grateful.
[code:1:e55fd47981]$sql = mysql_query("SELECT * FROM brochure_cats");
while($row = mysql_fetch_array($sql)) {
$id = $row['cat_id'];
$parent = $row['parent_id'];
$name = $row['cat_name'];
// Lets Get its Parent
$parent = mysql_query("SELECT * FROM brochure_cats where cat_id='$parent'");
while($row = mysql_fetch_array($parent)) {
$parent_id = $row['cat_name'];
$parent_name = $row['parent_id'];
$middle = mysql_query("SELECT * FROM brochure_cats WHERE cat_id='$parent_name'");
while($row = mysql_fetch_array($middle)) {
$middle_id = $row['cat_name'];
$selector = ' :';
$parent_output = $parent_id . $selector;
$output .= "<OPTION VALUE="$id">$middle $parent_output $name</OPTION> \n";
}
}
return $output;
}[/code:1:e55fd47981]
oh, p.s, the parent and middle bits pull up the info the wrong way round as you'll notice in the output element.
Many Thanks,
|