sneaky
I had a similar problem, had a three layer menu system that could be updated on the fly.
I had the menu items stored in the database (this also helps out a lot in creating a privilege system)
i have the table for menu items set up nice n simple like so:
+------------+------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------------------------+------+-----+---------+----------------+
| id | int(3) | | PRI | NULL | auto_increment |
| title | varchar(30) | YES | MUL | NULL | |
| link | varchar(100) | YES | | NULL | |
| type | enum('parent','child','sub_child') | YES | | NULL | |
| urlshow | varchar(15) | | | | |
| urlsubshow | varchar(25) | | | | |
| parent_id | int(3) | YES | | NULL | |
| position | int(2) | YES | | NULL | |
+------------+------------------------------------+------+-----+---------+----------------+
here is the code to grab the info from the database and populate it into a multi-dimensional array (I do this in the login script):
// load menu information from db into a global array
$get_menu = "select * from admin_menu order by type, parent_id, id, title";
$do_get_menu = mysql_query($get_menu);
$menu[parent] = array();
$menu[child] = array();
$menu[sub_child] = array();
while ($row=mysql_fetch_array($do_get_menu))
{
switch ($row[type])
{
case 'parent':
$position = $row[position];
$menu[parent][$position] = array($row[title],$row[link],$row[parent_id],$row[id],$row[urlshow],$row[urlsubshow]);
break;
case 'child':
$menu[child][] = array($row[title],$row[link],$row[parent_id],$row[id],$row[urlshow],$row[urlsubshow]);
break;
case 'sub_child':
$menu[sub_child][] = array($row[title],$row[link],$row[parent_id],$row[id],$row[urlshow],$row[urlsubshow]);
break;
}
}
session_register("menu");
now once this multi-dimensional array is stored as a session variable, i call a function simply called build_menu();
/**
* @return void
* @desc - variables provided<br>
* - $menu - multi-dimensional array containing all contents of the menu
* - this info is pulled for the array from the db at login (login.cinch)
* - $menu Structure
* - $menu[parent/child/sub_child][#][0] = title - title name to display
* - $menu[parent/child/sub_child][#][1] = link - hyperlink to reference to
* - $menu[parent/child/sub_child][#][2] = parent_id - id of parent (if child or subchild - for subchild, parent_id is actually the id of the child)
* - $menu[parent/child/sub_child][#][3] = id - id of this entry
* - $menu[parent/child/sub_child][#][4] = show - defined in this function, and decides what areas of the menu will be visible
* - $menu[parent/child/sub_child][#][5] = sub_show - same as $show, but for sub sections (currently only used within 'Products' section
* - $show
* - $show is defined in this function, and decides what areas of the menu will be visible
* - $sub_show
* - same as $show, but for sub sections (currently only used within 'Products' section
*/
function build_menu($menu,$show,$sub_show)
{
for ($i=0; $i<sizeof($menu[parent]); $i++)
{
// echo "parent $i<br>\n";
if ((isset($show) && $show == $menu[parent][$i][4]) || $show == "all")
{
if (eregi("http", $menu[parent][$i][1]))
{
echo "<a href='" . $menu[parent][$i][1] . "' target='new'>-</a> <a href='" . $menu[parent][$i][1] . "' target='new'>" . $menu[parent][$i][0] . "</a><br>\n";
}
else
{
echo "<a href='" . $menu[parent][$i][1] . "?show=" . $menu[parent][$i][4] . "&sub_show=" . $menu[parent][$i][5] . "'>-</a> <a href='" . $menu[parent][$i][1] . "?show=" . $menu[parent][$i][4] . "&sub_show=" . $menu[parent][$i][5] . "'>" . $menu[parent][$i][0] . "</a><br>\n";
}
for ($j=0; $j<sizeof($menu[child]); $j++)
{
if ($menu[child][$j][2] == $menu[parent][$i][3])
{
// echo "child $j<br>\n";
if (isset($sub_show) && $sub_show == $menu[child][$j][5] || $show == "all")
{
if (eregi("http", $menu[child][$j][1]))
{
echo " <a href='" . $menu[child][$j][1] . "' target='new'>-</a> <a href='" . $menu[child][$j][1] . "' target='new'>" . $menu[child][$j][0] . "</a><br>\n";
}
else
{
echo " <a href='" . $menu[child][$j][1] . "?show=" . $menu[child][$j][4] . "&sub_show=" . $menu[child][$j][5] . "'>-</a> <a href='" . $menu[child][$j][1] . "?show=" . $menu[child][$j][4] . "&sub_show=" . $menu[child][$j][5] . "'>" . $menu[child][$j][0] . "</a><br>\n";
}
for ($k=0; $k<sizeof($menu[sub_child]); $k++)
{
if ($menu[sub_child][$k][2] == $menu[child][$j][3])
{
// echo "sub-child $k<br>\n";
echo " <a href='" . $menu[sub_child][$k][1] . "?show=" . $menu[sub_child][$k][4] . "&sub_show=" . $menu[sub_child][$k][5] . "'>-</a> <a href='" . $menu[sub_child][$k][1] . "?show=" . $menu[sub_child][$k][4] . "&sub_show=" . $menu[sub_child][$k][5] . "'>" . $menu[sub_child][$k][0] . "</a><br>\n";
}
}
}
else
{
if (ereg("sawmill", $menu[child][$j][1]))
{
echo " <a href='" . $menu[child][$j][1] . "' target='new'>+</a> <a href='" . $menu[child][$j][1] . "' target='new'>" . $menu[child][$j][0] . "</a><br>\n";
}
else
{
echo " <a href='" . $menu[child][$j][1] . "?show=" . $menu[child][$j][4] . "&sub_show=" . $menu[child][$j][5] . "'>+</a> <a href='" . $menu[child][$j][1] . "?show=" . $menu[child][$j][4] . "&sub_show=" . $menu[child][$j][5] . "'>" . $menu[child][$j][0] . "</a><br>\n";
}
}
}
}
}
else
{
if (eregi("http", $menu[parent][$i][1]))
{
echo "<a href='" . $menu[parent][$i][1] . "' target='new'>+</a> <a href='" . $menu[parent][$i][1] . "' target='new'>" . $menu[parent][$i][0] . "</a><br>\n";
}
else
{
echo "<a href='" . $menu[parent][$i][1] . "?show=" . $menu[parent][$i][4] . "&sub_show=" . $menu[parent][$i][5] . "'>+</a> <a href='" . $menu[parent][$i][1] . "?show=" . $menu[parent][$i][4] . "&sub_show=" . $menu[parent][$i][5] . "'>" . $menu[parent][$i][0] . "</a><br>\n";
}
}
}
}
and that's it..
kinda archaic, and I haven't finished notating everything, so I apologize for the messiness of it all
-=Lazz=-