SQL based menu with submenues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Timtastic
Forum Newbie
Posts: 3
Joined: Mon Jun 21, 2010 6:30 pm

SQL based menu with submenues

Post by Timtastic »

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:

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>";
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: SQL based menu with submenues

Post by josh »

Before:

Code: Select all

'WHERE `parent_category` = 0'
After:

Code: Select all

'Where `parent_category` = ' . $id
The programming term for this operation is http://en.wikipedia.org/wiki/Concatenation

In PHP the concatenation operator is the period. You're joining two "strings" or pieces of text.
Timtastic
Forum Newbie
Posts: 3
Joined: Mon Jun 21, 2010 6:30 pm

Re: SQL based menu with submenues

Post by Timtastic »

The problem is that if $id is for example id 4. It must know the first parent id and open the right main category. How do i solve that?
Timtastic
Forum Newbie
Posts: 3
Joined: Mon Jun 21, 2010 6:30 pm

Re: SQL based menu with submenues

Post by Timtastic »

My code so far but i can't navigate the way i want to i the submenues. Can someone please guide me in the right direction?
php code :

Code: Select all

function displayChildren($parent,$orginal = "")
{
   $result = mysql_query("SELECT * FROM `kategorier` WHERE `parent_category` = $parent");
   echo "<ul>";
   while($item = mysql_fetch_object($result))
   {
  // echo "parent = $parent  id = $item->id";
   if ($orginal == $item->id) {
      echo "<li> - <a href=getcat.php?id=".$item->id."><b>" . $item->namn . "</b></a> id: ".$item->id."</li>";
	  } else {
	   echo "<li> - <a href=getcat.php?id=".$item->id.">" . $item->namn . "</a> id: ".$item->id."</li>";
	  }
	//  echo "orignal = $orginal. id = $item->id . Parent = $parent";
 if ($orginal == $item->id && $item->id != $parent) {
      displayChildren($item->id,$orginal);
	  }
   }
   echo "</ul>";
}
 function getmainid($parent)
 {
 $sql = mysql_query("SELECT * FROM `kategorier` WHERE `id` = $parent") or die (mysql_error());
 while($sqlrow=mysql_fetch_object($sql)){
 $nyttid = $sqlrow->parent_category;
 if ($sqlrow->type == "huvud") {
$id = $sqlrow->id;
 } else {
 $id = getmainid($nyttid);
 }
 }
return $id;
 }
// Skriver ut själva menyn
$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))
{
$currentid = getmainid($id);
if ($currentid == $item->id) {
   echo "<li><a href=getcat.php?id=".$item->id."><b>" . $item->namn . "</b></a> id: ".$item->id."</li>";
}else {
   echo "<li><a href=getcat.php?id=".$item->id.">" . $item->namn . "</a> id: ".$item->id."</li>";
}
   if ($item->id != $currentid)
continue;
   $cock = displayChildren($item->id,$id);
   print_r ($cock);
}
echo "</ul>";
Post Reply