#native_company# #native_desc#
#native_cta#

Dynamic Cascading Menu Tree

By Patrick M. McGovern
on November 9, 2004

Version: .2

Type: Function

Category: Other

License: GNU General Public License

Description: One base function for calling menu information from a db and building a functional cascading menu tree from it.

admin_menu table setup:
+------------+------------------------------------+------+-----+---------+----------------+
| 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    |                |
+------------+------------------------------------+------+-----+---------+----------------+

CREATE TABLE `admin_menu` (
  `id` int(3) NOT NULL auto_increment,
  `title` varchar(30) default NULL,
  `link` varchar(100) default NULL,
  `type` enum('parent','child','sub_child') default NULL,
  `urlshow` varchar(15) NOT NULL default '',
  `urlsubshow` varchar(25) NOT NULL default '',
  `parent_id` int(3) default NULL,
  `position` int(2) default NULL,
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `title` (`title`)
) TYPE=MyISAM;



<?php
//menu.php
mysql_connect($host, $user, $pass);
mysql_select_db('$db_name');

build_menu();
?> 

<?
//menu_function.php
/**
 * @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()
	{	
	$get_menu = mysql_query("select * from admin_menu");

	$menu[parent] = array();
	$menu[child] = array();
	$menu[sub_child] = array();

	while ($row=mysql_fetch_array($get_menu))
		{
		switch ($row[type])
			{
			case 'parent':
				$position = $row[position];
				$menu[parent][$position] = array($row[title],$PHP_SELF,$row[parent_id],$row[id],$row[urlshow],$row[urlsubshow]);
				break;
			case 'child': 
				$menu[child][] = array($row[title],$PHP_SELF,$row[parent_id],$row[id],$row[urlshow],$row[urlsubshow]);
				break;
			case 'sub_child': 
				$menu[sub_child][] = array($row[title],$PHP_SELF,$row[parent_id],$row[id],$row[urlshow],$row[urlsubshow]);
				break;
			}
		}

	if (!isset($show))
		{
		$show = "";
		}
		
	if (!isset($sub_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> &nbsp; <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> &nbsp; <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] && (isset($sub_show) && $sub_show == $menu[child][$j][5] || $show == "all"))
					{
					// echo "child $j<br>n";
					if (eregi("http", $menu[child][$j][1]))
						{
						echo "&nbsp; &nbsp; &nbsp; <a href='" . $menu[child][$j][1] . "' target='new'>-</a> &nbsp; <a href='" . $menu[child][$j][1] . "' target='new'>" . $menu[child][$j][0] . "</a><br>n";
						}
					else
						{
						echo "&nbsp; &nbsp; &nbsp; <a href='" . $menu[child][$j][1] . "?show=" . $menu[child][$j][4] . "&sub_show=" . $menu[child][$j][5] . "'>-</a> &nbsp; <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 "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href='" . $menu[sub_child][$k][1] . "?show=" . $menu[sub_child][$k][4] . "&sub_show=" . $menu[sub_child][$k][5] . "'>-</a> &nbsp; <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 (eregi("http", $menu[parent][$i][1]))
				{
				echo "<a href='" . $menu[parent][$i][1] . "' target='new'>+</a> &nbsp;<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> &nbsp;<a href='" . $menu[parent][$i][1] . "?show=" . $menu[parent][$i][4] . "&sub_show=" . $menu[parent][$i][5] . "'>" . $menu[parent][$i][0] . "</a><br>n";
				}
			}
		}
	}
?>