Andre
Andre

Reputation: 233

How to create a mysql php multi-level list navigation

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

Upvotes: 0

Views: 3394

Answers (4)

Rovshan Mamedov
Rovshan Mamedov

Reputation: 268

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.

Upvotes: 2

jeroen
jeroen

Reputation: 91792

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.

Upvotes: 1

Camille Hodoul
Camille Hodoul

Reputation: 386

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 ?

Upvotes: 0

Friendly Code
Friendly Code

Reputation: 1655

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>

Upvotes: 0

Related Questions