marksie1988 Posted August 27, 2013 Share Posted August 27, 2013 Hi Guys, I have the below code that I am trying to make work correctly, basically it creates a menu structure and then should only make the parent active if the page you are on belongs to that parent. at the moment the structure is wrong as it is adding too many <ul> entries for each menu. this is what it currently does: <ul id="nav"> <li class="active"><a href="admin.php">System Settings</a> <ul id="nav"> <li><a href="reports.php">Reports</a></il> </ul> <ul id="nav"> <li><a href="memblst.php?opt=createuser">Create User</a></il> </ul> </li> </ul> This is what it should be: <ul id="nav"> <li class="active"><a href="admin.php">System Settings</a> <ul> <li><a href="reports.php">Reports</a></il> <li><a href="memblst.php?opt=createuser">Create User</a></il> </ul> </li> </ul> $MENU["SYSTEMS"] = array ( 'parent'=>true, 'enabled'=>true, 'text'=>'System Settings', 'link'=> 'admin.php', 'sub_modules' => array('REPORTS','CREATE_USER') ); $MENU["REPORTS"] = array ( 'parent'=>FALSE, 'enabled'=>true, 'text'=>'Reports', 'link'=> 'reports.php', 'sub_modules' => array() ); $MENU["CREATE_USER"] = array ( 'parent'=>FALSE, 'enabled'=>true, 'text'=>'Create User', 'link'=> 'memblst.php?opt=createuser', 'sub_modules' => array() ); $MENU["MEMBER_LST"] = array ( 'parent'=>FALSE, 'enabled'=>true, 'text'=>'Member List', 'link'=> 'memberlst.php', 'sub_modules' => array() ); $MENU["SYSTEM_LOGS"] = array ( 'parent'=>FALSE, 'enabled'=>true, 'text'=>'System Logs', 'link'=> 'syslog.php', 'sub_modules' => array() ); function show_menu(&$MENU,$subIndex=false){ $menu_string = ''; $menu_string .= '<ul id="nav">'; if(!$subIndex){ foreach($MENU as $item) { if( $item['enabled']&&$item['parent'] ) { $_subString=""; if(!empty($item['sub_modules'])){ foreach($item['sub_modules'] as $sub){ $_subString .= show_menu($MENU,$sub); } } $menu_string .= '<li class="active"><a href="'.$item['link'].'">'.$item['text'].'</a>'.$_subString.'</li>'; } } }else{ if(@$MENU[$subIndex]['enabled']&&!@$MENU[$subIndex]['parent']) { $_subString=""; if(!empty($MENU[$subIndex]['sub_modules'])){ foreach($MENU[$subIndex]['sub_modules'] as $sub){ $_subString .= show_menu($MENU,$sub); } } $menu_string .= '<li><a href="'.$MENU[$subIndex]['link'].'">'.$MENU[$subIndex]['text'].'</a></il>'.$_subString; } } return $menu_string.'</ul>'; } echo show_menu($MENU); Quote Link to comment https://forums.phpfreaks.com/topic/281596-menu-parent-active/ Share on other sites More sharing options...
marksie1988 Posted August 29, 2013 Author Share Posted August 29, 2013 Is no one able to assist with this? Im really struggling to get it working. Or if you can point me in the direction of a similar method to create a dropdown menu that will mark the parent as active when using a sub menu that would be great. Quote Link to comment https://forums.phpfreaks.com/topic/281596-menu-parent-active/#findComment-1447255 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.