Welcome Guest, Not a member yet? Register   Sign In
Need help: dynamic menus from database entries
#1

[eluser]parham90[/eluser]
Hello there again,

I have been thinking on this for about a month now, and I have not gotten yet a clue of how to do this. I need to put the menu structure in a database table, possibly with fields such as id, text, address, parent_id. I thought if the parent_id is 0, the item is root (I.E. one of the main menu items). If it has a parent id, it is a submenu, and the parent's id is stored in its parent_id. There is a problem here though. As this list can go on to infinity, is there a way to figure out which item is the submenu of which? Should the complete menu be constructed from top to bottom (I.E. the very first menu item, its submenus, the second and then its submenus etc), or should it be from bottom to top? And once I figure out the structure, in what way should it be stored (E.G. a two-dimensional array, or a hash table, or what)?

Thanks a lot.
#2

[eluser]rwestergren[/eluser]
You could create a table of the menu relationships having a row for each.

menu_id | parent_id
0 | 0 /*root menu, no parent*/
1 | 0
2 | 0
3 | 1 /*parent is menu_id 1, who's parent is menu_id 0*/
#3

[eluser]parham90[/eluser]
Hi,

Yes, but the problem is the storing of menus somehow in PHP, so later on it can be used to create menus. Something like, off the top of my head:

Code:
$tree = array(
1 => array(
5 => null,
6 => null,
7 => array(
9 => null
),
),
2 => array(
10 => null
),
);

The table structure that would make such an array would be:

Code:
menu_id | parent_id
1       | 0
2       | 0
5       | 1
6       | 1
7       | 1
9       | 7
10      | 2

The problem is traversing the MySQL tree, and then having the tree turned into a structure that works and is easy to again traverse through, to create the links. So, I'd need a function for traversing the database, constructing a structure that contains the menu in its entirety, and then I'd need another function to traverse this PHP data structure, and echo to the browser as it goes.
#4

[eluser]Crimp[/eluser]
This is done using a recursive function to build an array of the menu levels - the tree. You then run another function to simply format this array into html. There are tuts on building a catalog in PHP that shows the technique.
#5

[eluser]WanWizard[/eluser]
Google on "mysql nested tree", you'll be amazed at what's out there...

You are approaching the problem from the wrong end. Just because you have a hierarchy ( a tree ) in your output doesn't mean your internal variable structure should reflect that. You make it overly complex.

You are starting from a flat structure (i.e. the result of a query), just loop through the results, keep track of the current tree depth, and generate the html accordingly. Remember, the end result (the html) is flat as well.
#6

[eluser]Jondolar[/eluser]
A "nested set" is the appropriate pattern here. Google it and read about how you can easily build your entire tree from one query to the database. Inserts are intensive but that is okay for a menu system (usually).
#7

[eluser]parham90[/eluser]
Hi there,

Thank you all. That helped a lot. The name was what I was not sure about, and what kept me from googling. Smile

Take care!
#8

[eluser]InsiteFX[/eluser]
Here, for any one wanting to know about nested trees.

Nested Trees with Class

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB