taymax Posted June 14, 2010 Share Posted June 14, 2010 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 More sharing options...
taymax Posted June 14, 2010 Author Share Posted June 14, 2010 This is what added and its working now 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>"; } Link to comment https://forums.phpfreaks.com/topic/204756-group-array-for-2-level-navigation/#findComment-1072010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.