Building a multi-Level menu in PHP

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
blaine
Super Advanced
Super Advanced
Posts: 59
Joined: Mon Apr 05, 2004 11:29 pm

Building a multi-Level menu in PHP

Post by blaine »

I have several menu's now being dynamically created by PHP and all is well. Here is my next challenge. I want to be able to create a menu that may be n-levels deep and I won't know if this item has submenu items in advance.

The way I've done this for a top level menu item is to use the
aI("text='.$label.';showmenu=mysubmenu;");

Where submenu1 is a JS function in my menu_data.php which uses PHP to create the menu items. That works well.

Now -- what if the 2nd item of this submenu is another submenu. I can't be creating more JS menu functions in real-time - at least thats not my plan. I need to be able to call a JS function with a parameter like:
aI("text='.$label.';showmenu=mysubmenu(pid);");


The other option I see is being able to generate the needed menu item structure without calling a JS function and generating it all in PHP. This is not a problem for standard menu items - but is for submenus'

Does this make sense - any ideas as I continue to try furhter ?
blaine
Super Advanced
Super Advanced
Posts: 59
Joined: Mon Apr 05, 2004 11:29 pm

Post by blaine »

Well, I have a working solution that is dymically creating the submenus. I was thinking there may be addmenu type methods but I don't see any in the docs.

If there is a better way - someone please let me know. If not, then maybe this code will help someone else. I place this in my menu_data.php to create a dynamic number of JS functions for the submenus. I then reference them in other code when I am generating the menu - reference these functions.

Code: Select all

<?php
/* Generate the JS Menu Functions that are now needed for each menu item that is a submenu */
$pquery = DB_query("SELECT DISTINCT pid FROM {$_TABLES['glmenu']} WHERE pid > 0");
while (list($pid) = DB_fetchArray($pquery)) {
    echo '
        with(milonic=new menuname("glmenu'.$pid.'")) { 
            style=menuStyle1;
            itemwidth="150";
            $query = DB_query("SELECT id,label,url FROM {$_TABLES['glmenu']} WHERE pid='{$pid}' ORDER BY menuorder");
            while (list ($id,$label, $url) = DB_fetchArray($query)) {
                if (DB_count($_TABLES['glmenu'],"pid",$id) > 0 ) {
                   echo 'aI("text='.$label.';showmenu=glmenu'.$id.';");';
                } else {
                    echo 'aI("text='.$label.';url='.$url.';");';
                }
            }
        echo '}';
}
?>
Additional refinements will be made in my final code but this is working.
Post Reply