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 parent menu

Status
Not open for further replies.

b3n82

Programmer
Nov 3, 2006
1
GB
Hi a couple of months back someone posted a menu that pulled its information from a database and displayed the parent and child values but could only drill down to one child

The code is below can anyone see how to make this go down more than one level.

Code:
<link REL="STYLESHEET" HREF="styles/css.css" TYPE="text/css">
<?
$host = "localhost";
$dbuser = "un";
$dbpassword = "pass";
$database = "db";

$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);
$getpagename = explode("other_pages/",$url);
$Itemid = $getitemid[1];
$newparent = $getparent[1];
$newtoplevel = $gettoplevel[1];
$pagename = $getpagename[1];

#echo $url;
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))
{
	$domain = strstr($gotmainmenu[3], 'index.php');
	echo "<tr align='left'><td><a href='[URL unfurl="true"]http://www.url.net/$gotmainmenu[/URL][3]"; if ($domain != ""){echo"&Itemid=$gotmainmenu[0]";} else {echo"?Itemid=$gotmainmenu[0]";} echo "' 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='[URL unfurl="true"]http://www.url.net/$gotsubmenu[/URL][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='[URL unfurl="true"]http://www.url.net/$gotsubsubmenu[/URL][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>';
?>
 
I'd put this bit:
Code:
    $submenu = mysql_query("select * from mos_menu where menutype = 'mainmenu' and published = '1' and parent = '[red]$gotmenu[/red]' 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='[URL unfurl="true"]http://www.url.net/$gotsubmenu[/URL][3]&Itemid=$gotsubmenu[0]&parent=$gotsubmenu[6]' class='sublevelmark' "; if ($getnewitemid[0] == $gotsubmenu[0]){echo "id='active_menumark'";} echo ">$gotsubmenu[2]</a></div>";    
        [red]// Add recursive call here[/red]
        }

into its own function with parameter $gotmenu (original call with $gotmainmenu[0]) and just let it recurse. Much easier than nesting loops.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top