Jump to content

Dynamic Menu


seran128

Recommended Posts

I have a table in my db called tbl_category

THe structure of the table is

CategoryID
ParentID
sort_order
category_description

the 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 like

Animals
-Birds
-Dogs
-Cat
Furniture
Toys

Now 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 returns

something like

Animals

Dogs

Animals
Cats

Animals
Birds

Animals
Reptiles

Animals
Horses

Animals
Rabbits

Animals
Gerbils

Animals
Hamsters

Animals
Art

Books
Link to comment
https://forums.phpfreaks.com/topic/28584-dynamic-menu/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/28584-dynamic-menu/#findComment-130831
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.