Skip to content

Instantly share code, notes, and snippets.

@sadanandkenganal
Last active April 3, 2022 12:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sadanandkenganal/5f60461112b051dde5ab to your computer and use it in GitHub Desktop.
Save sadanandkenganal/5f60461112b051dde5ab to your computer and use it in GitHub Desktop.
Add active class to menu in codeigniter.
@peterson-umoke
Copy link

this code is good but limited, its only looking a class not the method.

@peterson-umoke
Copy link

peterson-umoke commented Jan 28, 2017

<?php 
	if(!function_exists("show_current_class")) {
		function show_current_class($args = "",$class = "active") {
			$route_obj = new CI_Router;
			$get_page = $route_obj->method;

			if($get_page == $args) {
				echo $class;
			}
		}
	}
 ?>

this might be okay

@chrismartinez99
Copy link

This is great! Thank you! I've modified it a bit here based upon the uri_string. Here you go:

    function active_link($path, $className = 'active')
    {
        $CI         =& get_instance();
        $uri_string = $CI->uri->uri_string();

        // Home is usually at / && has 0 total segments
        if ($path === '/' && ($CI->uri->total_segments() === 0)) {
            $ret_val = 'active';
        } else {
            $ret_val = ($uri_string === $path) ? $className : '';
        }

        return $ret_val;

    }

@MohsinAhmedShaikh
Copy link

i use peterson-umoke code and its works on these lines

<li class="<?php echo show_current_class('addmovies'); ?>"><a href="addmovies">Add Movie</a></li>
<li class="<?php echo show_current_class('movieslist'); ?>"><a href="movieslist">Movies List</a></li>
<li class="<?php echo show_current_class('search_movie'); ?>"><a href="search_movie">Search Movies</a>

but could not add active class without have href following line

<a class="<?php echo show_current_class('Movies'); ?>" href="i dont have any page for this">

please help

my sidebar menu code are

<aside>
        <div id="sidebar"  class="nav-collapse ">
              <!-- sidebar menu start-->
              <ul class="sidebar-menu" id="nav-accordion">
                  <li>
                      <a href="index.php">
                          <i class="icon-dashboard"></i>
                          <span>Dashboard</span>
                      </a>
                  </li>
				 <li class="sub-menu">
                      <a class="<?php echo show_current_class('Movies'); ?>" href="i dont have any page for this">
                          <i class="icon-tasks"></i>
                          <span>Movies</span>
                      </a>
                      <ul class="sub">
                          <li class="<?php echo show_current_class('addmovies'); ?>"><a href="addmovies">Add Movie</a></li>
                          <li class="<?php echo show_current_class('movieslist'); ?>"><a href="movieslist">Movies List</a></li>
                          <li class="<?php echo show_current_class('search_movie'); ?>"><a href="search_movie">Search Movies</a></li>
                      </ul>
                  </li>
				  <li class="sub-menu">
                      <a href="javascript:;" class="<?php echo show_current_class('home'); ?>">
                          <i class="icon-tasks"></i>
                          <span>Tv Shows</span>
                      </a>
                      <ul class="sub">
                          <li><a href="addmovies">Add Tv Show</a></li>
                          <li><a href="movieslist">Tv Shows List</a></li>
                          <li><a href="search_movie">Search Tv Shows</a></li>
                      </ul>
                  </li>
                      </ul>
                  <!--multi level menu end-->
              <!-- sidebar menu end-->
          </div>
      </aside>

Thanks

@chunchillo
Copy link

chunchillo commented Jul 19, 2018

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('activate_menu')) {
  function activate_menu($link, $css = "") {
    if ( isset($_SESSION["link"]) ) {
        $controller = $_SESSION["link"];
    } else {
        $controller = "inicio";
    }
    return ($controller == $link) ? "{$css} active" : $css;
    unset($_SESSION["link"]);
  }
}
?>

In your controllers o methods put in

<?php
$this->session->set_flashdata("link", "name_of_menu_to_activate");
?>

In your menu boostrap

<li class="nav-item <?= activate_menu("name_of_menu_to_activate"); ?>">
    <a class="nav-link" href="#">link</a>
</li>

@a-tesh
Copy link

a-tesh commented Aug 23, 2019

I ended up with this. An optional method can be passed.

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('active_link')) {
  function activate_menu($controller, $function = null) {
    $CI = get_instance();
    $class = $CI->router->fetch_class();
    if($function) {
        $method = $CI->router->fetch_method();
        return ($method == $function && $class == $controller) ? 'active' : '';
    }
    
    return ($class == $controller) ? 'active' : '';
  }
}?>

@developerinusa
Copy link

developerinusa commented Feb 9, 2020

Hi all, i changed it with to array compatible.

<php if(!function_exists('active_link')) {
    function activate_menu($controller)
    {
        // Getting CI class instance.
        $CI = get_instance();
        // Getting router class to active.
        $class = $CI->router->fetch_class();
        $is_active = FALSE;
        if(is_array($controller)) {
            foreach ($controller as $cont){
               if($cont == $class){
                   $is_active = TRUE;
               }
            }
        }else{
            if($controller == $class){
                $is_active = TRUE;
            }
        }

        return ($is_active) ? "active" : "";
    }
}?>

Yo can also set active class to top menu like this

<?=activate_menu('controller');?>
<?=activate_menu(['controller1', 'controller2']);?>

@zdarin
Copy link

zdarin commented Apr 18, 2020

Hi all, i changed it with to array compatible.

<php if(!function_exists('active_link')) {
    function activate_menu($controller)
    {
        // Getting CI class instance.
        $CI = get_instance();
        // Getting router class to active.
        $class = $CI->router->fetch_class();
        $is_active = FALSE;
        if(is_array($controller)) {
            foreach ($controller as $cont){
               if($cont == $class){
                   $is_active = TRUE;
               }
            }
        }else{
            if($controller == $class){
                $is_active = TRUE;
            }
        }

        return ($is_active) ? "active" : "";
    }
}?>

Yo can also set active class to top menu like this

<?=activate_menu('firma','firmaType');?>

hi i cant figure out why not working in codeigniter 4.0.2 can you help ?

@orbitasol
Copy link

Hi all,
I think it is most easy to track active link by current url with js. Check the following link
Sorry for my bad english.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment