Codeigniter--add "active" css class to link, how to?


What's the quickest and easiest way to add the "active" class to a link, so it can be styled? I'm developing an app in CI, and I'd like a quick easy way to do this automatically.

jQuery is an option too...

12/22/2009 5:44:44 PM

Accepted Answer

You should really be using the CodeIgniter URI Class to do this instead of the $_SERVER['REQUEST_URI']


if ( $this->uri->uri_string() == '/contact' )

^^ that is the preferred way to do things due to some complexities that can happen with codeigniter's routing features

1/3/2010 10:10:54 AM

If you have a lot of navigation items you can do it this way (very simplified)...

<li<?= if ( $_SERVER['REQUEST_URI'] == '/contact' ): ?> id="active"<?php endif; ?>><a href="">contact</a></li>

You'll have to edit it for your needs...

If you don't have that many nav items an easier way is to give each page a body id and then use css to make it active.

<style type="text/css">
body#contact #contact-nav { font-weight:bold; }

<body id="contact">

<ul id="navigation">
    <li id="contact-nav"><a href="">contact</a></li>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow