Recursing Multilevel menu (PHP/MYSQL)

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
sagstuen
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2004 3:36 pm

Recursing Multilevel menu (PHP/MYSQL)

Post by sagstuen »

I've been working on a multi-level menu for my webshop project for some time, and posted some questions about it. The hints I got pointed me to recusing... As my knowledge of PHP is somewhat intermediate, I had some trouble figuring out this recusion thing, but I'm learning. This is what I did, and it works just fine for me... Hope it helps some of you out there.

What it does is this:

I have a table who stores my menu-items or group-names as I like to call them. The following table should be self-explaining:

CREATE TABLE `t_groups` (
`groupe_id` int(11) NOT NULL auto_increment,
`groupe_name` varchar(30) NOT NULL default '',
`groupe_parent` int(11) NOT NULL default '0',
`groupe_url` varchar(255) NOT NULL default '',
`groupe_show` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`groupe_id`),
FULLTEXT KEY `groupe_navn` (`groupe_navn`)
)


I'm using a style-sheet to format the table-cells...
The following function does the dirty work:

Code: Select all

<?php
  function menu_level($parent, $indent, $connection)
  {
    if (isset($parent))
    {
      $query = "SELECT * FROM t_groups WHERE group_parent = $parent  AND group_show = 1 ORDER BY group_name";
    }

    if (!($result = @ mysql_query ($query, $connection)))
      showerror();
    else
    {
      while ($row = mysql_fetch_array($result))
      {
        echo "<TR>";
        echo "  <TD CLASS='menu' STYLE=font-size:8pt; font-family:verdana; text-align:left;'>";
        echo "    <a class='menu' href='index.php?page=prodliste&group=" . $row['group_id'] . "'>";
        echo "      <p align='left' STYLE='text-indent:  $indent ; font-weight: bold'>" . $row['group_name'];
        echo "    </a>";
        echo "  </TD>";
        echo "</TR>";

        $sub_query = "SELECT * FROM t_groups WHERE group_parent = " . $row['group_id'] . " AND group_show = 1 LIMIT 0, 1";
        if ($sub_result = @ mysql_query ($sub_query, $connection))
        {
          $sub_indent = $indent + 10;
          menu_level($row['group_id'], $sub_indent, $connection);
        }
      }
    }
  }?>
This is the code going into the actual page where the menu is supposed to apear:

Code: Select all

<TABLE CLASS='block' STYLE="vertical-align:top">
<TR>
  <TD CLASS='heading'>GROUPS
  </TD>
</TR>
<?php
  if (!($connection = @ mysql_pconnect ($sql_server, $sql_user, $sql_pwd)))
    showerror();
  if (!(mysql_select_db ($sql_db, $connection)))
    showerror();
  menu_level(0,0,$connection);
?>
</TABLE>
I hope this is of some use... Feel fre to comment me on this one...
User avatar
Pamela
Forum Newbie
Posts: 3
Joined: Mon Jan 13, 2003 7:29 am
Location: Fairbanks, Alaska

Post by Pamela »

Do you have a URL that shows this in action?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Could also be done with a single JOIN query (and separate tables per level).

Might be good to get the html out of the script - and use CSS rather than a <table>.
Post Reply