seran128 Posted November 27, 2006 Share Posted November 27, 2006 I have a table in my db called tbl_categoryTHe structure of the table isCategoryIDParentIDsort_ordercategory_descriptionthe way the table is structured is that each item has a CategoryID.The problem is that say I have a category_description "Animals" with a CategoryID of "1" ParentID of "0"Then I have a category_description "Birds" with a CategoryID of "2" ParentID of "1"Then I have a category_description "Dogs" with a CategoryID of "3" ParentID of "1"Then I have a category_description "Cat" with a CategoryID of "4" ParentID of "1"The problem is that say I have a category_description "Furniture" with a CategoryID of "5" ParentID of "0"The problem is that say I have a category_description "Toys" with a CategoryID of "6" ParentID of "0"I want the menu to lokk likeAnimals-Birds-Dogs-CatFurnitureToysNow my code[code]function getcategorymenu(){ global $connection; $sql=mysql_query("select * from tbl_category ORDER BY sort_order")or die(mysql_error()); while($row=mysql_fetch_array($sql)) { $getcategorymenu .= "<table width='200' border='0'> <tr> <td>" . stripslashes($row['category_description']) . "</td> </tr> <tr> <td></td> </tr> </table>"; $sqls = mysql_query("select category_description from tbl_category WHERE CategoryID = '$row[ParentID]' ORDER BY sort_order") or die(mysql_error()); while($rows=mysql_fetch_array($sqls)) { $getcategorymenu .="<table width='200' border='0'> <tr> <td>" . stripslashes($rows['category_description']) . "</td> </tr> </table>"; } }return $getcategorymenu;}[/code]Currently this code returnssomething likeAnimals Dogs Animals Cats Animals Birds Animals Reptiles Animals Horses Animals Rabbits Animals Gerbils Animals Hamsters Animals Art Books Quote Link to comment Share on other sites More sharing options...
Hypnos Posted November 27, 2006 Share Posted November 27, 2006 Wouldn't something like this work?[code]$getcategorymenu = "":while($row=mysql_fetch_array($sql)) { if($row['ParentID'] == 0) $getcategorymenu .= stripslashes($row['category_description']) . "<br />"; else $getcategorymenu .= " - " . stripslashes($row['category_description']) . "<br />";}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.