Forum Moderators: coopster

Message Too Old, No Replies

endless menu

         

supermanjnk

2:11 pm on May 16, 2005 (gmt 0)

10+ Year Member



I'm using a menu using ul's(a little javascript but this isn't JS related) I have all my menu's items stored in a database. My table structure is.

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?

StupidScript

7:08 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about using a "parent" field and then dynamically assessing that item's "level" during page generation?

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. ;)

supermanjnk

8:57 pm on May 16, 2005 (gmt 0)

10+ Year Member



The Category field is the same as the parent field, just with a different name. I'm trying to make it so I don't have to specify a while statement for each one. so I don't if they have three levels, and they want four I don't have to add another query for level four.

StupidScript

9:26 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like you're making some kind of content management system. So ... you're looking for a code approach that accomodates one user who makes a menu with 2 levels and another who makes one with 15 levels ... or 24 or whatever.

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.

supermanjnk

4:05 pm on May 19, 2005 (gmt 0)

10+ Year Member



Thanks For the help, I got it working how i needed.

StupidScript

5:02 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whadja end up with? (If you don't mind me asking)

ergophobe

5:27 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Typically, the way to do this is recursion, but watch out for infinite recursion or just many many levels.

You could write a hasChildren() function and then recurse to the next level for cases where there are children.

supermanjnk

5:35 pm on May 19, 2005 (gmt 0)

10+ Year Member



I can't show my code at this time, but maybe when the project is done I will.