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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.