Recursive menu

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am having some trouble with a recursive function that displays a
menu. In my database I have two fields that matters: page_id and
parent_id. Displaying the menu correctly is not a problem, but I need
it to expand the subpages of a parent page only when the user is
reading the parent page of a set of subpages, or one of the subpages,
or one of the subpages' subpage etc.. When the user is reading the
frontpage, only the pages with parent_id = 0 should be displayed.

Does anyone have a reasonable solution to this problem?

Re: Recursive menu

Quoted text here. Click to load it

This is code from my site, I don't go into subpages on my page menus,
but my sitelist does, I specify the root page at the beginning and in
goes through the menu tree.

It should work the way you want by just specifying your page ID.

here's how it looks on my site:

hope thats useful

database table, url:

  `url_id` varchar(9) NOT NULL default '',
  `url_name` varchar(40) NOT NULL default '',
  `url_location` varchar(60) NOT NULL default '',
  `url_description` varchar(255) NOT NULL default '',
  `url_parents` varchar(50) NOT NULL default '',
  `url_path` varchar(80) NOT NULL default '',
  PRIMARY KEY  (`url_id`)

id - is a text based id (i.e. photos')
name - a text name (what shows on the menu
location - the file name of the PHP page
description - a description of the page
parents - in my site pages can have multiple parents, which the text
IDs are csv in the field
path - default menu path for the entry (i.e. main-cbmidx-bbsidx ),
this gives a default trackback of menus to the main menu.  (if someone
calls it direct, otherwise I have GET provide a trackback.)

Te root menu ID (url_id) is "main", so I call to generate the site


function ulist($iname) {
    $record = dbquery(
        "SELECT *
        FROM url
        WHERE url_id='".$iname."' LIMIT 1");
    $item = mysql_fetch_array($record);
    echo "
        <b><a href=\"".$item['url_location']."\">".
    echo "<br />
    $match = dbquery(
        "SELECT *
        FROM url
            url_parents LIKE '%".$item['url_id']."%'
            AND url_id != 'main'
        ORDER BY url_name");
    if( mysql_num_rows($match) > 0){
        while( $list = mysql_fetch_array($match) ){
    echo "</ul>";

Re: Recursive menu

On Dec 8, 11:00 pm, wrote:
Quoted text here. Click to load it

Typically you can solve this in a lateral way by using the uri's
directory structure to decide when to expand a submenu.  It usually
leads to a cleaner site structure and is more SEO friendly to boot.

My 2 cents,

Site Timeline