2

I have created an array with my menu items, which includes sub menus but i'm not sure how to add an active class if the url matches.

I need to add an active class to both

<li class="treeview">

and the <li> under <ul class="treeview-menu">

my array contains the link key in nested arrays as below

any help will really be appreciated

'menu-name' => array(
    'Top Level 1' => array(
        'icon' => 'fa-volume-control-phone',
        'Sub Item' => array(
            'link' => '/?seq='.$seq,
            'icon' => ' fa-asterisk',
        ),
        'Sub Item' => array(
            'link' => '/voip/?seq='.$seq,
            'icon' => 'fa-history',
        ),
    ),
    'Devices &amp; Numbers' => array(
        'link' => '/?seq='.$seq,
        'icon' => 'fa-phone',
    ),
    'Tools' => array(
        'icon' => 'fa-wrench',
        'Account Settings' => array(
            'link' => '/?seq='.$seq,
            'icon' => 'fa-cog',
        ),
        'Delete Account' => array(
            'link' => '/?seq='.$seq,
            'icon' => 'fa-trash',
        ),
    ),
),

if(!function_exists("show_side_menu")) {
    function show_side_menu($menu) {
        global $navigation;

        if(!empty($navigation[ $menu ])) {
            $ret = '';
            foreach($navigation[ $menu ] as $a => $b) {
                if(menu_permissions($a, get_logged_in_customer()) or $menu != 'main_menu') {
                    if(isset($b["link"])) {
                        $ret.= '<li class="treeview">';
                        $ret.= '<a href="'.$b["link"].'">';
                        $ret.= '<i class="fa '.$b["icon"].'"></i> ';
                        $ret.= '<span onClick="OpenPage(\''.$b["link"].'\');">'.$a.'</span> ';
                        $ret.= '</a>';
                        $ret.= '</li>';
                    } else {
                        $ret.= '<li>';
                        $ret.= '<a href="javascript:void(0);">';
                        $ret.= '<i class="fa '.$b["icon"].'"></i> ';
                        $ret.= '<span>'.$a.'</span> ';
                        $ret.= '<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>';
                        $ret.= '</a>';
                        $ret.= '<ul class="treeview-menu">';
                        foreach($b as $c => $d) {
                            if(is_array($d)) {
                                $ret.= '<li>';
                                $ret.= '<a href="'.$d["link"].'">';
                                $ret.= '<i class="fa '.$d["icon"].'"></i> ';
                                $ret.= '<span onClick="OpenPage(\''.$d["link"].'\');">'.$c.'</span> ';
                                $ret.= '</a>';
                                $ret.= '</li>';
                            }
                            elseif($d == 'title') {
                                $ret.= '<li class="lead" style="color:#ffffff; margin-left:12px; padding-top:15px;">'.$c.'</li>';
                            }
                        }
                        $ret.= '</ul>';
                        $ret.= '</li>';
                    }
                }
            }
        } else {
            $ret = false;
        }
        return $ret;
    }
}
charlie
  • 415
  • 4
  • 35
  • 83
  • 1
    Possible duplicate of [Add class="active" to active page using PHP](https://stackoverflow.com/questions/13336200/add-class-active-to-active-page-using-php) – ALFA Oct 11 '19 at 07:47
  • What have you tried so far? Where are you stuck? – Nico Haase Oct 15 '19 at 13:41

1 Answers1

0

You should check the Uri inside the function, please try this code

$uri = $_SERVER['REQUEST_URI'];

inside the loop you call, check the current URI is equal to your link, then you should add the class after checking, like this,

    if ($b['link'] == $uri) {
        $class = 'your-active-class';
    } else {
       $class = '';
    }

    $ret.= '<a href="'.$b['link'].'" class="'.$class.'">'

I hope you understand this

  • Suppose you are in `something.php?param1=3`, `$uri` would be `something.php?param1=3` while `$b['link']` would be `something.php`, which are different even if they should match the active class. – ALFA Oct 11 '19 at 07:42
  • @ALFA also, the active class has to be added to the parent – charlie Oct 12 '19 at 09:34