Skip to content Skip to sidebar Skip to footer

How Can I Set Class="active" To Navigation Menu In Codeigniter?

I just start up to be a web developer. Now I create a dynamic website for the first time. I don't know how to set class='active' to the navigation menu. Here is my menu code:

Solution 1:

You can use $this->uri->segment();

<li><ahref="<?phpecho base_url(); ?>patient/createpatient"<?phpif($this->uri->segment(1)=="menu_name"){echo'class="active"';}?> ><iclass="fa fa-users fa-lg"></i> Create Patient </a></li><?php } ?><li><ahref="<?phpecho base_url(); ?>patient/listpatient"<?phpif($this->uri->segment(1)=="menu_name"){echo'class="active"';}?> ><iclass="glyphicon glyphicon-list-alt fa-lg"></i> List Patients </a></li><?phpif( $usertype == "Admin"){?><li><ahref="<?phpecho base_url(); ?>user/"<?phpif($this->uri->segment(1)=="menu_name"){echo'class="active"';}?> ><iclass="fa fa-list fa-lg"></i> List Users </a></li>

Solution 2:

I do this on most projects. How I achieve this is like this;

<ahref="<?phpecho site_url('patient/listpatient'); ?>"class="<?phpif($this->uri->uri_string() == 'patient/listpatient') { echo'active'; } ?>"><iclass="glyphicon glyphicon-list-alt fa-lg"></i> List Patients</a>

It's matching the current uri string, with the links href. If it matches, it add's an active class to the link.

Hope this helps.

I have also used site_url over base_url

Solution 3:

Try this one. I think no need of javascript or jquery.

If you are using codeigniter then you can use URI Class. Get the menu from url using

$this->uri->segment();  

and apply to you code like below

<li><ahref="<?phpecho base_url(); ?>patient/listpatient"><iclass="glyphicon glyphicon-list-alt fa-lg <?phpif($this->uri->segment(1)=="memu_name"){echo"active";}?>"></i> List Patients </a></li>

Solution 4:

I have used Codeigniter URI Class $this->uri->segment();

Please look at my code:

<liclass="<?=($this->uri->segment(1)==='technology')?'active':''?>"><ahref="<?phpecho base_url('technology')?>">Tech</a></li>

Please note....

class="<?=($this->uri->segment(1)==='technology')?'active':''?>"

Also you can use URI Class $this->uri->uri_string()

Here is my active class for home page(index)

<liclass="<?=($this->uri->uri_string()=== '')?'active':''?>"><ahref="<?phpecho base_url('')?>">Home</a></li>

Solution 5:

<ahref="<?=site_url('contact')?>"<?phpif($this->uri->segment(1)=="contact") {echo'class="active nav__item-link"';}?>class="nav__item-link">Contact Us</a>

Post a Comment for "How Can I Set Class="active" To Navigation Menu In Codeigniter?"