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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With