Have a look here:
http://phpclasses.gremlins.com.hk
There are a number of classes which deals with these tree menus.
--
Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
/*
I do not fear computers. I fear the lack of them.
-- Isaac Asimov
*/
I wrote the code below for a menu system like this on a recent project:
----------
// declare class (s)elect (l)ist
class sl
{
var $query;
var $result;
var $row;
function set_query($new_value)
{
$this->query = $new_value;
}
}
// create object (c)ategory (s)elect (l)ist
$csl = new sl();
// create object (s)ubcategory (s)elect (l)ist
$ssl = new sl();
// set query for object csl to display main categories
$csl->set_query("SELECT ID, Name FROM Categories WHERE ParentID=0");
$csl->result = mysql_query($csl->query);
// display the menu
while ($csl->row = mysql_fetch_array($csl->result))
{
$ParentCatID=$csl->row["ID"];
if ($csl->row["ID"] == $MainCatID)
{
echo("<p class=\"menuselected\">" .
"<a href=$PHP_SELF?MainCatID=$ParentCatID>" .
$csl->row["Name"] . "</a></p>\n");
// set query for object (s)ubcategory (s)elect (l)ist
$ssl->set_query("SELECT ID, Name, ParentID FROM Categories " .
"WHERE ParentID=$MainCatID");
$ssl->result = mysql_query($ssl->query);
// display subcategories (submenu)
while ($ssl->row = mysql_fetch_array($ssl->result))
{
$ParentCatID = $ssl-row["ParentID"];
$ChildCatID = $ssl->row["ID"];
if ($ssl->row["ID"] == $SubCatID)
{
echo("<p class=\"submenuselected\">" .
"<a href=$PHP_SELF?MainCatID=" .
"$ParentCatID&SubCatID=$ChildCatID>" .
$ssl->row["Name"] . "</a></p>\n");
}
else
{
echo("<p class=\"submenu\">" .
"<a href=$PHP_SELF?MainCatID=" .
"$ParentCatID&SubCatID=$ChildCatID>" .
$ssl->row["Name"] . "</a></p>\n");
}
}
}
// finish main category display
else
{
echo("<p class=\"menu\">" .
"<a href=$PHP_SELF?MainCatID=$ParentCatID>" .
$csl->row["Name"] . "</a></p>\n");
}
}
----------
Main menu categories have a ParentID assignment of 0 in the db. A select is
done of all ID & Name in the Categories table where ParentID=0. The menu is
displayed with names linked to recalling the script and passing the variable
$MainCatID -- the ID of the category. When the script is called with
$MainCatID set (when a main menu category is selected), the menu name with
ID matching $MainCatID is assigned a style class to distinguish visually
that it's selected. Also at this point another selection is done from
Categories where ParentID=$MainCatID, thus creating the list of
subcategories for the selected main category. The subcategories are also
displayed with links back to the script passing the variable $MainCatID
again plus the variable $SubCatID. When the script is called with $SubCatID
set, the submenu name with ID equal to $SubCatID is also class-styled to
indicate the selection.
The main page display (in this case for a products catalog) is generated by
other selection queries based on $MainCatID and $SubCatID.
--
Lowell Allen