PHP & MySQL - Help with menus

Associate
Joined
14 Dec 2005
Posts
563
Location
Dublin
I have a php script for generating horizontal menus on my application - both 'main' and submenus.

I have two database tables for my menues:

table1.gif


target_name is used so I can tell what page i'm generating a menu on. I use a class - view php design so target_name is actually the name of a controller file I call.. target_id is the primary key pointing to..

table2.gif


the foreign key 'menu_target_id' in my menu table. menu_id is auto increment. submenu_of is used to specify that the menu item is the child of another menu_id.

menu_type is 1 for main menu/top row, 2 is submenu. 3 and 4 arent used for now..

PHP:
function drawMainmenu($pTarget) { 

	global $db;
	
	$vTarget = $pTarget;

	$vLinks = $db->get_results("SELECT menu.menu_id AS menu_id, menu.menu_target_id As menu_target_id, target.target_id AS target_id, target.target_name AS target_name, menu.menu_type AS menu_type, menu.link_text AS text, menu.script_name AS link, menu.menu_params AS parameters, menu.submenu_of AS submenu_of FROM target_menu target, menues menu WHERE target_id = menu_target_id");
	
	echo '<div id="mainmenu">';
	echo '<ul>';
	
	foreach($vLinks as $vTempLinks) { ?>
		<li>
			<?php 
				
			if (!$vTempLinks->submenu_of) {	
				if ($vTempLinks->target_name == $vTarget) {
					echo "<a href=$vTempLinks->link.php$vTempLinks->parameters class=active >$vTempLinks->text</a></li>";
				}
				else {
					echo "<a href=$vTempLinks->link.php$vTempLinks->parameters>$vTempLinks->text</a></li>";
				}
			}
				
		 if ($vTempLinks->target_name == $vTarget) {
			drawSubMenu($vTempLinks->menu_id);	
		}
	}
	
	echo '</ul>';
	echo '</div>';

}

function drawSubMenu($pMainmenuId) {
	global $db;
	
	echo "passed id: ".$pMainmenuId;
	$vLinks = $db->get_results("SELECT menu_id AS menu_id, menu_type AS menu_type, link_text AS text, script_name AS link, menu_params AS parameters, submenu_of as submenu_of FROM menues WHERE submenu_of = $pMainmenuId ORDER BY menu_order");
	
	if ($vLinks) {
		//echo "<pre>"; print_r($vLinks); echo "</pre>";
		echo '<div id="submenu">';
		echo '<ul>';
		
		foreach($vLinks as $vTempLinks) { ?>
			<li>
				<?php 
						echo "<a href=$vTempLinks->link.php$vTempLinks->parameters>$vTempLinks->text</a></li>";
		}
		
		echo '</ul>';
		echo '</div>';	
	}
}

And that's the code to generate the menus. It generates the top level mainmenu fine, and highlights whichever page you're currently on.

The reason i'm posting this thread is because i'm unsure about how to get my submenu working. I am pretty sure my SQL statement for submenu is wrong.

My menu looks like this when generated:

Home Products Forum Admin

As specified in the database, Admin has 3 submenu items (3 with submenu_of id specified) These 3 items are "User Management" "Css Upload" and "Documents"

My submenus appear fine when I click Admin, BUT if i click into any of the submenu items other than User Management, the submenu disappears!

Menu highlighting for the submenus isnt implemented yet either.

Can anyone help me sort this out? My SQL isn't brilliant and i've sorta got myself lost and can't figure out how to sort this all out :\
 
Back
Top Bottom