如何在codeigniter的动态菜单列表中添加活动类?

时间:2018-09-15 13:00:24

标签: php codeigniter

我想在单击任何菜单时在当前URL中显示<li class = "active">

这是我尝试过的:

<?php   
    $uri_string = $this->uri->uri_string();
    $id_user = $usr->id;
    $get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
    $hasil = $get_group->result();
    foreach($hasil as $h)       
    if(isset($h->group_id)){
        $in_group = $this->ion_auth->in_group($h->group_id);
        if(isset($in_group)){
            $get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
            $menu = $get_menu->result();
            foreach($menu as $m){
                $cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));                              
                if($cekSub->num_rows() > 0){                                        
                    echo '<li>';                                        
                    echo '                                  
                      <a href="javascript:void(0);" class="menu-toggle">
                        <i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>                                       
                      </a>
                      <ul class="ml-menu">
                        <li>';
                        foreach($cekSub->result() as $c)
                        echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
                        echo '</li>
                      </ul>       
                    </li>';
                } else {
                    echo '<li>';
                    echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
                    echo '</li>';
                }
            }
        }           
    }           
?>

问题是,如果不在当前URL类中,则活动代码被隐藏-仅显示<li>

该如何解决?

1 个答案:

答案 0 :(得分:1)

已解决:

<?php 
                $getUri = $this->uri->uri_string();
                $id_user = $usr->id;
                $get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
                $hasil = $get_group->result();
                foreach($hasil as $h)       
                if(isset($h->group_id)){
                    $in_group = $this->ion_auth->in_group($h->group_id);
                    if(isset($in_group)){       
                        $get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
                        $menu = $get_menu->result();
                        foreach($menu as $m){
                            $cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));  
                            $getSub = $cekSub->result();

                            if($cekSub->num_rows() > 0){                                    
                                echo '<li';                     
                                    foreach($getSub as $c)
                                    if($c->controller_link==$getUri){echo ' class="active"';}
                                echo '>
                                  <a href="javascript:void(0);" class="menu-toggle">
                                    <i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>                                       
                                  </a>
                                  <ul class="ml-menu">
                                    <li>';
                                    foreach($getSub as $c)
                                    echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
                                    echo '</li>
                                  </ul>       
                                </li>';
                            } else {
                                echo '<li';
                                    if($m->controller_link==$getUri){echo ' class="active"';}
                                echo '>';
                                echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
                                echo '</li>';
                            }
                        }

                    }           
                }           
            ?>