0

Basically I want to be able to create a multi-level navigation (many sub navs). Obviously I know this will be done through creating lists with in each other but I am pretty stuck on the logic of displaying it correctly.

I have seen stuff regarding parent/children relationships but can't find anything that is efficient and easy to udnerstand.

I don't need to know how the HTML is built. Just how the php/mysql can generate the lists.

Hope you can help.

A

Andre
  • 233
  • 8
  • 24
  • 1
    What are you looking for help on, specifically? The MySQL table structure? The PHP code? The HTML code? – daiscog Oct 10 '11 at 15:33
  • The php code and mysql query that can generate the mutli level navigation. I know how to make it statically already but I need to know how to create it from a database. – Andre Oct 10 '11 at 15:45
  • See [this post](http://stackoverflow.com/questions/7687110/mysql-db-driven-menu-generator-function/7687554) – daiscog Oct 10 '11 at 15:48

4 Answers4

2

Here is code I used. It builds unordered list with unlimited level of subitems.

/*
* Table has 3 fields: `ID`, `PARENTID` and `NAME`
* `ID` is unique, `PARENTID` showing his parent node id.
* This function will go through it and build unordered list and call itself when needed to build subitems.
* $level argument used to define wich node's subitems to build. Default is 0 which is top level.
*/

function showMenu($level = 0) {

$result = mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$level); 
echo "<ul>";
    while ($node = mysql_fetch_array($result)) { 
        echo "<li>".$node['NAME'];
        $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$node['ID'])) != null;
        IF ($hasChild) {
            showMenu($node['ID']);
        }
        echo "</li>";
    }
echo "</ul>";
}

Hope that helps.

Rovshan Mamedov
  • 270
  • 2
  • 12
1

I think the most efficient would be to get all records in one go from the database and then build the hierarchical structure again in php.

So you would have a structure similar to this in your database:

id    parent_id    menu_item

Then you can get all items and use a recursive function to build a hierarchical array which you can loop through to get your menu, sub-menu, sub-sub-menu, etc. items. See this question and the top-two answers on how to re-build the structure.

Community
  • 1
  • 1
jeroen
  • 91,079
  • 21
  • 114
  • 132
0

If you mean the HTML it's like this:

<ul>
   <li>
      <a href="#">Title</a>
      <ul>
         <li><a href="#">Title</a></li>
         <li><a href="#">Title</a></li>
         <li><a href="#">Title</a></li>
      </ul>
   </li>
   <li><a href="#">Title</a></li>
   <li><a href="#">Title</a></li>
   <li><a href="#">Title</a></li>
</ul>
Friendly Code
  • 1,585
  • 1
  • 25
  • 41
0

assuming you know how to create filled with the content of a mysql table assuming you have the following tables : Universes > Categories > Markets > Segments

1) list the content of 'Universes' in a select. when the user picks, call another .php script and send it the id of the chosen Universe (using GET or POST)

2) list the content of 'Categories', WHERE idUniverses = the id you sent to the second script.

3) same for the Markets...

It's easier with AJAX.

need the code ?

Camille Hodoul
  • 376
  • 2
  • 13