Mysql Php dynamic menu

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello there,
I am trying to write a script that will create dynamically a menu from MySQL  
The database table is a product categories table and it has the folowing  
category_id, parent_category_id, category_name (some other fileds are there  
as well but these are enough to figure out the logic)

I know that I have to store all the product_categories records in a temp  
array and then some how go through them and store the level of each record  
in the menu....

If you have done something like that I suppose you know what I mean and I  
would much appreciate to give me a hint here ...
I had a look in the phpLayersMenu from sourceforge but it is really advanced  
OO code and I cannot understand the logic....

My target is to create a Tree menu with no pre - defined number of  

Thanks in advance.  

Re: Mysql Php dynamic menu

I don't know how far back to start an answer. Do you know how to create
a regular, non-tree, dynamic menu from the MySQL DB for only the top
level categories (i.e. the categories with parent_category_id=0)?

Re: Mysql Php dynamic menu UPDATE

I have writen a class that does what I wanted except the UL LI part...  
anyway .I am going to paste you the code here.
The logic of this script is part of phpLayersMenu in sourceforge but much  
If at anytime you redevelop that code, I would like to send me the changes  

As far as I know it can display unlimited levels of menus.

the only problem with the ULs and LIs is that in order to make a DHTML or  
plain CSS menu
You need the following structure:
My script,
Closes every LI
so it creates the folowing:
(THAT NEEDS fixing to create UL LIs as above)

class menu
 var $parent_id=0;
 var $level=0;
 var $tree = array();
 var $nodesCount=0;
 var $firstItem=1;
 var $lastItem=0;
 var $maxLevel=0;
 var $firstLevelCnt=0;
 var $i=0;
 var $x = array();

 function menu()
  $this->nodesCount = 0;
  $this->tree = array();
  $this->treecnt = array();
  $this->maxLevel = 0;
  $this->firstLevelCnt = array();
  $this->firstItem = array();
  $this->lastItem = array();

 function scanTableForMenu()
  $dbresult = mysql_query('SELECT * FROM product_categories
      WHERE product_categories_id != 0
  $this->tmpArray = array();
  while ($row = mysql_fetch_array($dbresult))
   $this->tmpArray[$row['product_categories_id']]['parent_id'] =  
   $this->tmpArray[$row['product_categories_id']]['name'] =  
   $this->tmpArray[$row['product_categories_id']]['body'] =  
   $this->tmpArray[$row['product_categories_id']]['img'] =  
  echo "<ul>";
  echo "</ul>";
 function depth($tmpArray,$parent_id,$level)
  reset ($tmpArray);
  foreach($tmpArray as $key => $value)
   if ($value['parent_id'] == $parent_id)
    $cnt = count($this->tree) + 1;
    $this->tree[$key]['level'] = $level;
    $this->tree[$key]['parent'] = $value['parent_id'];
    $this->tree[$key]['name'] = $value['name'];
    $this->tree[$key]['body'] = $value['body'];
    $this->tree[$key]['img'] = $value['img'];
    if($key !=$parent_id)
 function displayMenu($tree,$currentLevel = 0)
  //echo "<pre>";
  //echo "</pre>";

  while (list($key, $value) = each($this->tree))// for($ii = 1;$ii  
    echo "<ul>";
   if($this->tree[$key]['level'] == $currentLevel)
    echo "<li>";
     echo  "<a href=\"?cat_id=$key\">".$this->tree[$key]['name']."</a>";

   elseif($this->tree[$key]['level'] == $currentLevel+1)
     echo "</li>";
     echo "</ul>";

Re: Mysql Php dynamic menu UPDATE

Following on from Angelos's message. . .
Quoted text here. Click to load it

It is one thing to be able to generate a 'tree' menu with nodes, sub  
nodes etc but another to be able to open and shut dynamically using  
javascript.  What I have done is to write a class (actually 2 classes )  
that interfaces with Tigra Tree ['free']  
(  I've added the ability  
to use templates at different levels.  The test program below should  
give you a flavour.  Ask if you want the code (11k too big for news)

   menu tree

   This example populates a 2-level tree showing employees
   by department.  It illustrates templates for links.

   Note the order in which items are added to the tree.
   Always add item to tree/parent node BEFORE adding children to it
   Nodes are added as references so unset() before re-using a node

   Files required
     graphic files as specified in tree_tpl.js



// dummy data
$e1=array(1=>'Peter fox',2=>'Sally Fox',3=>'Geoffrey Fox');
$e2=array(11=>'Teddy fox',12=>'Jean Fox');

// set up tree and organise templates
$t = new menuTree();

// highest level is always shown and doesn't collapse
$tnode = new treeNode('Employees by department');

// populate tree
foreach($departments as $did=>$dept){
   $dnode = new treeNode($departmentName,array('DID'=>$did));
   foreach($employeesOfDept as $eid=>$employeeName){
     $dnode->AddChild(new treeNode($employeeName,array('EID'=>$eid)));

// illustrate bypassing template using blank for data key
$snode = new treeNode('something special',array(''=>'myhomepage.php'));

// show it


PETER FOX Not the same since the poster business went to the wall
2 Tees Close, Witham, Essex.
Gravity beer in Essex  <

Site Timeline