Jump to content

Group array for 2 level navigation


taymax

Recommended Posts

Hi

 

Im trying to make a 2 level navigation. Ive looked at a few ideas online but they seem too compilcated

 

Here is my my sql data in a function

 

 

		function get_side_nav(){

	$sql = "SELECT * FROM side_nav";
	$res = mysql_query($sql)or die(mysql_error());

	$sideNavArr = array();

	while($row = mysql_fetch_array($res))
	  { 
	  
	  $sideNavArr[] = array('idside_nav' => $row['idside_nav'],
							'label' => $row['label'],
					   		'link' => $row['link_url'],
							'online' => $row['online'],
	  						'parent_id' => $row['parent_id']);
	  }
	  return $sideNavArr;
	}

 

 

Here is what i've done that seprates the menu from sub menu. where $side_item['parent_id'] == 0 its a menu else its a sub

 

$menu = array();
$sub_menu = array();
foreach($sideNavArr as $side_item){
if ($side_item['parent_id'] == 0){
		$menu[] = array('idside_nav' =>$side_item['idside_nav'],
						'label' =>$side_item['label'],		
						'link' =>$side_item['link']);
	}else{
		$sub_menu[] = array('parent_id' =>$side_item['parent_id'],
						'label' =>$side_item['label'],		
						'link' =>$side_item['link']);
	}
}

 

Current output:

Array ( [0] => Array ( [idside_nav] => 2 [label] => 2 [link] => # ) [1] => Array ( [idside_nav] => 3 [label] => 3 [link] => # ) )

Array ( [0] => Array ( [parent_id] => 2 [label] => Nav 1 [link] => ?pid=1 ) [1] => Array ( [parent_id] => 2 [label] => 4 [link] => # ) [2] => Array ( [parent_id] => 2 [label] => 5 [link] => # ) )  

 

Now the question is how do i put the sub menu under the main menu items

Link to comment
https://forums.phpfreaks.com/topic/204756-group-array-for-2-level-navigation/
Share on other sites

This is what added and its working now  :D

 

foreach($menu as $menu_item){
echo "<ul>".$menu_item['label'];

foreach($sub_menu as $sub_menu_item){
	if($sub_menu_item['parent_id'] == $menu_item['idside_nav'])
	echo "<li>".$sub_menu_item['label']."</li>";
}
echo "</ul>";
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.