I have a menu in my view:
<ul>
<li>
<a href="<?php echo base_url(); ?>news" <?php if($nav_item == 'news'): ?>class="active-nav-item"<?php endif; ?>>News about Cars</a>
</li>
<li>
<a href="<?php echo base_url(); ?>products" <?php if($nav_item == 'products'): ?>class="active-nav-item"<?php endif; ?>>Latest Products</a>
</li>
<li>
<a href="<?php echo base_url(); ?>media" <?php if($nav_item == 'media'): ?>class="active-nav-item"<?php endif; ?>>Audio/Video</a>
</li>
</ul>
And I am sending to this view a variable $nav_item from my controller e.g. news (I have controllers for every item in the navigation so: news, products and media controller) like this:
class News extends CI_Controller {
public function index()
{
$data['nav_item'] = 'news'; // variable for navigation
$data['main_content'] = 'pages/news';
$this->load->view('template', $data);
}
}
So, it is working fine, but the code in a link is really long:
<a href="<?php echo base_url(); ?>news" <?php if($nav_item == 'news'): ?>class="active-nav-item"<?php endif; ?>>News about Cars</a>
I would like to make a function from this where I send only two parameters and it will do other things automatic. E.g. it could look like this:
<a href="<?php echo base_url(); ?>news" <?php activeitem($navitem,'news'); ?>>News about Cars</a>
So, if $navitem == 'news' it echoes "class='active-nav-item'"
How to do this function so I can put it in my e.g. myown_helper.php in helpers folder.
Thanks in advance.
Function for the helper could be:
Then on the link just do: