Forum Moderators: coopster
id, title, level, category
so for instance
level 1 item one
level 2 -sub item one
level 3 --sub sub item one
level 2 -sub item two
level 3 --sub sub item one
level 3 --sub sub item two
level 4 ---sub sub sub item 3
level 1 item two
level 2 -sub item one
level 3 --sub sub item one
level 3 --sub sub item two
etc...
I want to make it so that this can be as deep as the person wants it without having to add more code each time, unfortunantly I can't figure out a way to do this. I have code that will get what the the last level
$query = mysql_query ( " SELECT * FROM items WHERE enabled='1'" );
while ($get_query = mysql_fetch_object($query)){
$level = $get_query->level;
if (!isset($l)){
$l = "0";
}
if ( $level > $l){
$l = $level;
} else {
}
}
(which in this case will return 4)
but if I had another sub menu level it would return 5, is there anyway to make it so that the menu can be completely dynamic?
id = 1 parent = 0 linktext = "Home" linkuri = "index.html" id = 2 parent = 1 linktext = "About Us" linkuri = "about.html" id = 3 parent = 1 linktext = "Contact Us" linkuri = "contact.html" id = 4 parent = 0 linktext = "Our Products" linkuri = "prods.html" id = 5 parent = 4 linktext = "Blue Widgets" linkuri = "widgets_blue.html" id = 6 parent = 5 linktext = "Blue Round Widgets" linkuri = "widgets_blue_round.html" then:
query1=select * from table where parent=0 while(q1=mysql_fetch_array(query1)) query2=select * from table where parent=q1["id"] while(q2=mysql_fetch_array(query2)) query3=select * from table where parent=q2["id"] etc.
Each nested query indicates another "level", organically. Top-level, secondary-level, etc. This could be used to write the appropriate indentation code.
(I know this ain't syntactically correct, I'm just laying out a concept. ;)
The problem is compounded in such a system in that you have no idea in what order the menu items will be included/removed/re-included in the database. It could easily end up that all of the top-level menu items are the first db entries, with their children scattered wildly through the rest of the table.
Have you determined a means to include every submenu item for, say, a level 3 entry without checking the status of every record in the table before including the level 3 entry and its children? Normally, this would be some kind of while loop.
Perhaps you could dump the entire menu as it is in the table into an array, and then do foreach's on the array elements instead of doing while loops with db calls.
There needs to be some kind of loop-checking to get all of the children.
Maybe another field or even another table just for controlling and accessing the depth of the menu, like when the user makes a level 0 menu, the menu table is updated to indicate only 1 level, and so only one while loop is used. Each time another level is added, another while loop is added to the writing routine.
I'm trying to picture writing a dynamic menu with an unpredictable number of levels without a while loop ... or at least a foreach loop looping through an array of the entire table content dump.