Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active menu tabs with codeigniter

Which method can be used in Codeigniter to achieve active menu tabs?

For example I have a user menu Profile Friends Messages Logout

When I'm in the profile controller the URL is domain.com/profile/some_function The Profile tab should be active by changing the background color for example.

I would usually add php to the tabs to check for their corresponding URL segment and I would change the style of the active tab. In Codeigniter should I implement a helper? What function can I easily use in tabs?

like image 397
CyberJunkie Avatar asked Jun 28 '11 18:06

CyberJunkie


1 Answers

As you said in your last paragraph, I would just check for the corresponding URI segment and change the style of the tab based on that.

Let's say you're using a foreach statement to generate your menu from a database:

<ul>
<?php foreach($menu_items as $menu_item):?>   
    <li<?php if($this->uri->segment(2) == url_title($menu_item->name, dash, TRUE)):?> class="active"><?php else:?>><?php endif;?><a href="<?=$menu_item->url;?>"><?=$menu_item->name;?></a></li>
<?php endforeach;?>
</ul>

The reason I threw url_title() in there is to make the name lowercase as the URI segment would be and take care of any possible spaces that may be in the menu name and/or URI segment. This would require the URL helper to be enabled. If they don't match, the default CSS for <li> elements would be used.

like image 121
Fuseblown Avatar answered Oct 01 '22 18:10

Fuseblown