Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic menu with parent and child

Status
Not open for further replies.

benarkely

Programmer
Oct 11, 2006
3
GB
Hi I need help completing a parent and child menu structure.

I can successfully display all of the parent items and even the children to that specific parent. I have even got the CSS applied to the menu, the problem is that when I click onto one of the children the parent does not remain open.

Code:
$host = "localhost";
$dbuser = "user";
$dbpassword = "pass";
$database = "dev-connect";

$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>";
	
	$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))
	{
		if ($Itemid == $gotsubmenu[6])
		{	
			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>';

Please any advice as I think I am close to getting this to work.
 
you're not giving us much to work on. have you a site where this behaviour can be observed?

unless your code is throwing errors it's difficult for us to re-engineer your experience as we don't have your css, your db schema or sample data.

if your code is throwing errors then let us know what those errors are.

if you are using css for the flyout's then it is unlikely that you will be able to maintain the flyout once a child has been clicked and the page refreshed. i've worked around this in the past with ajax but it was clunky and since i had to use js for the ajax i, in the end, gave up and use js for the menus too.
 
here is the db

Code:
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

it is possible to maintain the menu structure once the parent has been clicked as I can see this on different sites, the problem is how can I do it.

There aren't any errors that are produced as the menu correctly takes me to pages but does not keep the menu open.

If you could let me know what you'd like to view and I'll send it over.
 
I assume each page includes the file you show us as code? If so, right before the include, it could set a variable for which menu to have open (or maybe that's what you're trying to do with the explode(url) statement?). If so, maybe you need to add "?parent=" to your clickable links, and parse that off too.

A couple nits:

Put each statement on its own line. Much easier to read.

Refer to your db columns by name, not number. $gotsubmenu['published'] is much easier to read than $gotsubmenu[6], and protects you against future programmers who might add columns without noticing all your indexes.
 
thanks for the reply Miros,

Your right the explode statement is taking the id from the URL, I'll give the parent= URL extension a try and see what happens.

Thanks for the other little tips will bear that in mind from now on.

Cheers
 
sure you've seen sticky menus in other sites. i'll bet they use javascript for the opening and closing of menu items tho. not pure css.
 
hi ben I've had a look at this should work for you

Code:
$host = "localhost";
$dbuser = "user";
$dbpassword = "pass";
$database = "dev-connect";

$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);
$getparent = explode("parent=",$url);
$gettoplevel = explode("toplevel=",$url);
$Itemid = $getitemid[1];
$newparent = $getparent[1];
$newtoplevel = $gettoplevel[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>";
	
	$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))
	{
	
		if (($Itemid == $gotsubmenu[6]) || ($newparent == $gotsubmenu[6]))
		{	
		
			$getnewitemid = explode("&parent=",$Itemid);
			echo "<div style='padding-left: 4px'><a href='$gotsubmenu[3]&Itemid=$gotsubmenu[0]&parent=$gotsubmenu[6]' class='sublevelmark' "; if ($getnewitemid[0] == $gotsubmenu[0]){echo "id='active_menumark'";} echo ">$gotsubmenu[2]</a></div>";	
		
		}
		
		$subsubmenu = mysql_query("select * from mos_menu where menutype = 'mainmenu' and published = '1' and parent = '$gotsubmenu[0]' order by ordering asc");
		while($gotsubsubmenu = mysql_fetch_array($subsubmenu))
		{
		
			$getnewitemid1 = explode("&parent=",$Itemid);
			$getnewparent = explode("&toplevel=",$newparent);
			if (($getnewitemid1[0] == $gotsubsubmenu[6]) || ($getnewparent[0] == $gotsubsubmenu[6]))
			{	
			
				echo "<div style='padding-left: 12px'><a href='$gotsubsubmenu[3]&Itemid=$gotsubsubmenu[0]&parent=$gotsubsubmenu[6]&toplevel=$gotsubmenu[6]' class='sublevelmark' "; if ($getnewitemid1[0] == $gotsubsubmenu[0]){echo "id='active_menumark'";} echo ">$gotsubsubmenu[2]</a></div>";	
			
			}
		
		}
		
	}
	
	echo"</td></tr>";

}
echo '</table></td></tr></table>';

this works for one level down, I've also tried to add a further level down, which works to an extent.

The problem is that once the 3rd level menu item is pressed it only shows 1st and 3rd level but does not show the full tree if you know what I mean.

Can anyone help with this??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top