Welcome Guest, Not a member yet? Register   Sign In
Creating a menu
#1

[eluser]giovannidc[/eluser]
I've create a menu that looks similar to this:

Code:
<ul class="primary-menu">
  <li>Home</>
  <li>Packages</>
     <ul class ="secondary-menu">
        <li>Product A</li>
        <li class="active">Product B</li>
        <li>Product C</li>
     </ul>
  </li>
</ul>

Any ideas on how I could add the class="active" to the desired list item depending on the controller being called?

Thanks in advance!
#2

[eluser]InsiteFX[/eluser]
PHP Menu Active
#3

[eluser]Philo01[/eluser]
If you use page names in the url you can use the URI class to add the active state:

Code:
<ul class="primary-menu">
  <li>Home</>
  <li>Packages</>
     <ul class ="secondary-menu">
        <li &lt;?php echo ($this->uri->segment(1, false) == 'product-a') ? 'class="active"' : null; ?&gt;>Product A</li>
        <li &lt;?php echo ($this->uri->segment(1, false) == 'product-b') ? 'class="active"' : null; ?&gt;>Product B</li>
        <li &lt;?php echo ($this->uri->segment(1, false) == 'product-c') ? 'class="active"' : null; ?&gt;>Product C</li>
     </ul>
  </li>
</ul>

Works easier if your pages come from a database so you can just add this to the loop.

--

URI Class http://ellislab.com/codeigniter/user-gui...s/uri.html
#4

[eluser]louisl[/eluser]
Pretty much what Philo01 said, but I use a little helper function then use it all over the place, I got a bunch of other useful funcs in there too.
Code:
<li&lt;?php echo class_selected($this->uri->segment(1), 'menu-item'); ?&gt;>Menu item</li>

&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function class_selected($str1, $str2) {

if ($str1 == $str2) {

  return ' class="selected" ';
  
}

}

/* End of file MY_text_helper.php */
/* Location: ./application/helpers/MY_text_helper.php */
#5

[eluser]CroNiX[/eluser]
You can use:
$RTR->get_class() and $RTR->get_method() to get the, well, current class and method called Smile
#6

[eluser]giovannidc[/eluser]
Thanks guys for responding so fast and providing helpful suggestions,

I made use of Philo01's suggestion :coolsmile:




Theme © iAndrew 2016 - Forum software by © MyBB