Highlighting current tab in Codeigniter

1 minute read

On a current web application that I was working on I had to find a way to select the tab that the user is currently working in.  I thought about using jQuery to do this, but thought that there had to be a much simpler way to accomplish this!   After a little digging around the net, I found that Codeigniter makes this task quite easy.  All you have to do is get the name of the controller that you are in and this can be accomplished with the following line of code:

$this->uri->segment(1);

Once you know the name of your controller, all you have to do is use an if statement to check to see if you are in a specific controller and if so then attach an id to the element used to display your tabs.  In my code, I have a common un-ordered list with element:

if($this->segment(1) == 'admin') { echo 'id="current_tab"' };

Afterwards, all you have to do is to set up a CSS rule to change the color of the tab like this:

#current_tab {
  color: white;
  background-color: black;
}

I love how easy this is with Codeigniter, makes me enjoy working with Codeigniter even more!!

Leave a comment