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

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


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

$this->uri->uri_string()

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


Depends on how you're outputting your link HTML.

If you're using the URL Helper module, then you can call the anchor() function to create your links, and pass it an array of attributes as the third parameter, ie:

$this->load->helper('url');echo anchor('url/path', 'Click here', array('class' => 'active'));

If you're just outputting the HTML manually in your templates/views, obviously you can just create the class attribute yourself in the HTML.


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

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

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; }</style><body id="contact"><ul id="navigation">    <li id="contact-nav"><a href="">contact</a></li></ul>