PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Feb 23, 2018 3:29 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Feb 08, 2004 7:57 pm 
Offline
Forum Newbie

Joined: Sat Feb 07, 2004 4:36 pm
Posts: 4
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:
Syntax: [ Download ] [ Hide ]
<?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:

Syntax: [ Download ] [ Hide ]
<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...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 12:18 pm 
Offline
Forum Newbie
User avatar

Joined: Mon Jan 13, 2003 8:29 am
Posts: 3
Location: Fairbanks, Alaska
Do you have a URL that shows this in action?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 2:08 pm 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
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>.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group