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

3
  • 1
    What are you looking for help on, specifically? The MySQL table structure? The PHP code? The HTML code? Commented Oct 10, 2011 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. Commented Oct 10, 2011 at 15:45
  • See this post Commented Oct 10, 2011 at 15:48

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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>

Comments

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 ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.