Creating dynamic sub menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brob
Forum Newbie
Posts: 5
Joined: Tue Oct 03, 2006 5:48 am

Creating dynamic sub menu

Post by brob »

Hi can anyone help.

here is the code for the menu

Code: Select all

$host = "localhost";
$dbuser = "un";
$dbpassword = "pass";
$database = "dev-cwc";

$link = mysql_connect($host,$dbuser,$dbpassword);

$db_selected = mysql_select_db($database);
if (!$db_selected) {
   die ('Can\'t select database : ' . mysql_error());
}
$getitemid = explode("Itemid=",$url);
$Itemid = $getitemid[1];

echo '<table cellpadding="0" cellspacing="0" class="moduletabletestmenu"><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0">';

$mainmenu = mysql_query("select * from mos_menu where menutype = 'mainmenu' and published = '1' and parent = '0' order by ordering asc");
while($gotmainmenu = mysql_fetch_array($mainmenu))
{
	echo "<tr align='left'><td><a href='/$gotmainmenu[3]&Itemid=$gotmainmenu[0]' class='mainlevelmark' "; if ($Itemid == $gotmainmenu[0]){echo "id='active_menumark'";} echo ">$gotmainmenu[2]</a>";
	
	$getlevel = mysql_query("select * from mos_menu where id = '$Itemid'");
	$gotlevel = mysql_fetch_row($getlevel);
	#echo "$gotlevel[6]<br>";
	
	$submenu = mysql_query("select * from mos_menu where menutype = 'mainmenu' and published = '1' and parent = '$gotmainmenu[0]' order by ordering asc");
	while($gotsubmenu = mysql_fetch_array($submenu))
	{	

		echo "<div style='padding-left: 4px'><a href='/$gotsubmenu[3]&Itemid=$gotsubmenu[0]' class='sublevelmark' "; if ($Itemid == $gotsubmenu[0]){echo "id='active_menumark'";} echo ">$gotsubmenu[2]</a></div>";

	}
	echo"</td></tr>";
}
echo '</table></td></tr></table>';
and here is the mysql table

Code: Select all

Field	Type	Null	Default
id	int(11)	No	
menutype	varchar(25)	Yes	NULL
name	varchar(100)	Yes	NULL
link	text	Yes	NULL
type	varchar(50)	No	
published	tinyint(1)	No	0
parent	int(11) 	No	0
componentid	int(11) 	No	0
sublevel	int(11)	Yes	0
ordering	int(11)	Yes	0
checked_out	int(11) 	No	0
checked_out_time	datetime	No	0000-00-00 00:00:00
pollid	int(11)	No	0
browserNav	tinyint(4)	Yes	0
access	tinyint(3) 	No	0
utaccess	tinyint(3) 	No	0
params	text	No
I have got the main level to work correctly and it will show the sub menu's, but when you click on the sub menu it doesn't keep the top level open to show the sub level anymore.

This isn't a css issue its something to do with the second while loop.

Please I have a mental block and can't figure it out.

Please help
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Here is a nice little tutorial which does exactly what you want to do : http://www.phpwebcommerce.com/shop-browse-category.php
brob
Forum Newbie
Posts: 5
Joined: Tue Oct 03, 2006 5:48 am

Post by brob »

I wasn't really able to use this, is there anything else you can suggest.

thanks
Post Reply