How to add active class to menu and tab in codeigniter?

In codeigniter it’s easy to add active class to current menu or highlight current page in menu item.

i am using helper to active current menu or add active class to menu in codeigniter.

Create a file named “menu_helper.php” under “application/helpers/menu_helper.php” and add below code.

File name : menu_helper.php

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
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();
return ($class == $controller) ? 'active' : '';
}
}?>

Load above helper in autoload (go to config/autoload.php and add below code or add “menu” helper in existing helpers)

$autoload['helper'] = array('menu');

Add class to your menu. pass your controller name in activate_menu()

<li class="<?php echo activate_menu('home'); ?>"><a href="<?php echo site_url();?>">Home</a></li>
<li class="<?php echo activate_menu('contact'); ?>"><a href="<?php echo site_url('contact');?>">Contact us</a></li>

Make an active class in css or rename return ($class == $controller) ? ‘active’ : ”; active to your class to add on activation link.

Now go to your menus and click on it you get highlighted your menu with active class you added.





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here