1

In order to apply the active class to the current item in the navigation menu. I need to check if its link is equal to the URL

I am trying like this:

<ul class="nav">
    <li class="<?php if(uri_string(current_url()) == base_url() || uri_string(current_url()) == '') echo 'active'; ?>">
         <a href="<?php echo base_url() ?>">Home</a>
    </li>
    <li class="<?php if(uri_string(current_url()) == base_url('news/create/')) echo 'active'; ?>">
        <a href="<?php echo base_url('news/create/') ?>">+ New</a>
    </li>
</ul>

Wich seems to work fine with the home

But in news/create won't ever be equal....

It compares news/create (uri_string(current_url()) with /news/create (base_url('news/create'))

So.. what's the way to go with this issue?

3
  • 2
    Why not just do if($this->uri->uri_string() == 'news/create') ?
    – Mudshark
    May 8, 2013 at 14:01
  • Looks cleaner to me! Post it as an answer May 8, 2013 at 15:55
  • Just posted the answer :)
    – Mudshark
    May 9, 2013 at 16:38

2 Answers 2

4

As I said, a clean and simple solution:

if($this->uri->uri_string() == 'news/create'){ ...
2

Try this..

<ul class="nav">
    <li class="<?php if(! $this->uri->segment(1) || $this->uri->segment(1) == 'welcome') echo 'active'; ?>">
         <a href="<?php echo base_url() ?>">Home</a>
    </li>
    <li class="<?php if($this->uri->segment(1) == 'news' && $this->uri->segment(2) == 'create' ) echo 'active'; ?>">
        <a href="<?php echo base_url('news/create/') ?>">+ New</a>
    </li>
</ul>
3
  • It's simple.. That's what counts ;) May 8, 2013 at 14:01
  • I do agree with him/her. You just need to know which controller and action is called! May 8, 2013 at 14:12
  • 2
    My suggestion above, if($this->uri->uri_string() == 'news/create')is slightly less messy though :)
    – Mudshark
    May 8, 2013 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.