Mysql Php dynamic menu

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angelos

    Mysql Php dynamic menu

    Hello there,
    I am trying to write a script that will create dynamically a menu from MySQL
    database.
    The database table is a product categories table and it has the folowing
    filed:
    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_categor ies 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
    records...

    Thanks in advance.


  • petermichaux@yahoo.com

    #2
    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)?

    Comment

    • Angelos

      #3
      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
      simpler.
      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:
      <ul>
      <li>Category
      <ul>
      <li>SubCategory </li>
      <li>SubCatego ry
      <ul>
      <li>SubSubCateg ory</li>
      <li>SubSubCateg ory</li>
      </ul>
      </li>
      <li>SubCategory </li>
      </ul>
      </li>
      <li>Category
      <ul>
      <li>SubCategory </li>
      <li>SubCategory </li>
      </ul>
      </li>
      </ul>
      *************** *************** ***********
      My script,
      Closes every LI
      so it creates the folowing:
      (THAT NEEDS fixing to create UL LIs as above)
      <ul>
      <li></li>
      <ul>
      <li></li>
      <li></li>
      <ul>
      <li></li>
      </ul>
      <li></li>
      </ul>
      </ul>

      *************** ***********
      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->firstLevelCn t = array();
      $this->firstItem = array();
      $this->lastItem = array();
      }

      function scanTableForMen u()
      {
      $dbresult = mysql_query('SE LECT * FROM product_categor ies
      WHERE product_categor ies_id != 0
      ');
      $this->tmpArray = array();
      while ($row = mysql_fetch_arr ay($dbresult))
      {
      $this->tmpArray[$row['product_catego ries_id']]['parent_id'] =
      $row['product_catego ries_parent_id'];
      $this->tmpArray[$row['product_catego ries_id']]['name'] =
      $row['product_catego ries_name'];
      $this->tmpArray[$row['product_catego ries_id']]['body'] =
      $row['product_catego ries_body'];
      $this->tmpArray[$row['product_catego ries_id']]['img'] =
      $row['product_catego ries_img'];
      }
      $this->depth($this->tmpArray,0,0 );
      echo "<ul>";
      $this->displayMenu($t his->tree);
      echo "</ul>";
      }
      function depth($tmpArray ,$parent_id,$le vel)
      {
      reset ($tmpArray);
      foreach($tmpArr ay as $key => $value)
      {
      if ($value['parent_id'] == $parent_id)
      {
      unset($tmpArray[$key]);
      unset($this->tmpArray[$key]);
      $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'];
      unset($value);
      if($key !=$parent_id)
      $this->depth($this->tmpArray,$key, $level+1);
      }
      }
      }
      function displayMenu($tr ee,$currentLeve l = 0)
      {
      //echo "<pre>";
      //print_r($this->tree);
      //echo "</pre>";


      while (list($key, $value) = each($this->tree))// for($ii = 1;$ii
      <=$this->cntFirstlevel; $ii++)
      {
      if($this->tree[$key]['level']>$currentLeve l)
      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 )
      {
      $this->displayMenu($t his->tree,$currentL evel+1);
      }
      else
      {
      for($i=0;$i<$cu rrentLevel;$i++ )
      {
      echo "</li>";
      echo "</ul>";
      }
      $this->displayMenu($t his->tree,$currentL evel=0);
      }
      unset($this->tree[$key]);
      }
      }
      }


      Comment

      • Peter Fox

        #4
        Re: Mysql Php dynamic menu UPDATE

        Following on from Angelos's message. . .[color=blue]
        >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
        >simpler.
        >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.[/color]

        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']
        (www.softcomplex.com/products/tigra_menu_tree/). 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)


        <?php
        /*
        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
        ../cl/menutree.phpc
        ../tools/mnu/tree.js
        ../tools/mnu/tree_tpl.js
        graphic files as specified in tree_tpl.js

        */


        require_once('. ./cl/menutree.phpc') ;

        // dummy data
        $departments=ar ray();
        $e1=array(1=>'P eter fox',2=>'Sally Fox',3=>'Geoffr ey Fox');
        $e2=array(11=>' Teddy fox',12=>'Jean Fox');
        $departments[1]=array('Sales', $e1);
        $departments[10]=array('Product ion',$e2);


        // set up tree and organise templates
        $t = new menuTree();
        $t->AddLinkTemplat e(1,'');
        $t->AddLinkTemplat e(2,'department .php?DEPT=[DID]');
        $t->AddLinkTemplat e(3,'employee.p hp?EMPNO=[EID]');

        // highest level is always shown and doesn't collapse
        $tnode = new treeNode('Emplo yees by department');
        $t->AddNode($tnode );

        // populate tree
        foreach($depart ments as $did=>$dept){
        $departmentName =$dept[0];
        $dnode = new treeNode($depar tmentName,array ('DID'=>$did));
        $tnode->AddChild($dnod e);
        $employeesOfDep t=$dept[1];
        foreach($employ eesOfDept as $eid=>$employee Name){
        $dnode->AddChild(new treeNode($emplo yeeName,array(' EID'=>$eid)));
        }
        unset($dnode);
        }

        // illustrate bypassing template using blank for data key
        $snode = new treeNode('somet hing special',array( ''=>'myhomepage .php'));
        $tnode->AddChild($snod e);

        // show it
        print($t->CompleteMenu() );

        ?>


        --
        PETER FOX Not the same since the poster business went to the wall
        peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
        2 Tees Close, Witham, Essex.
        Gravity beer in Essex <http://www.eminent.dem on.co.uk>

        Comment

        Working...