Jump to content

Menu parent active


marksie1988

Recommended Posts

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);
Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.