SQL based menu with submenues
Posted: Mon Jun 21, 2010 6:39 pm
Hi, ive been having a problem with my menu. I can print the menu out with all the submenues without any problems. But, when i want to show a specific category and its submenues i get stuck. I'm not quite sure how the "code" should look. This is what i have so far.
SQL Table looks like this
ID NAME parent_category
1 some_name 0
2 some_name2 0
3 sub_category 1
4 sub_sub_category 3
And lets say i'm in category (id) 4. How do i only display id 1 -> 3 -> 4 ?
I would like to change my menu as well and store everything in an array so i dont have to run so many queries... I have a total mess so far.
Any help is appreciated!
PHP CODE SO FAR:
SQL Table looks like this
ID NAME parent_category
1 some_name 0
2 some_name2 0
3 sub_category 1
4 sub_sub_category 3
And lets say i'm in category (id) 4. How do i only display id 1 -> 3 -> 4 ?
I would like to change my menu as well and store everything in an array so i dont have to run so many queries... I have a total mess so far.
Any help is appreciated!
PHP CODE SO FAR:
Code: Select all
function displayChildren($parent)
{
$result = mysql_query("SELECT * FROM `kategorier` WHERE `parent_category` = $parent");
echo "<ul>";
while($item = mysql_fetch_object($result))
{
echo "<li> - " . $item->name . "</li>";
displayChildren($item->id);
}
echo "</ul>";
}
///// $_GET ID CODE IS WHAT I WANT TO BE ABLE TO USE BELOW ///
$id = $_GET["id"];
$menu = mysql_query("SELECT * FROM `kategorier` WHERE `parent_category` = 0") or die (mysql_error());
echo "<ul>";
while($item = mysql_fetch_object($menu))
{
echo "<li>" . $item->name . "</li>";
displayChildren($item->id);
}
echo "</ul>";