Reputation: 22777
I came across a similar issue as this user: How to add active class to codeigniter hyperlinks?
and the answer was to insert the following into the .php view page:
<a class="<?php if($this->uri->segment(1)=="search"){echo "active";}?>" href="<?=base_url('search')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
When I insert this into my page, even if my URL is "search" it does not assign the class "active" to the link tag. I tried doing this:
<?php
$uri = $this->uri->segment(1);
echo "<script type='text/javascript'>alert('$uri');</script>";
?>
and it alerts nothing (the alert box does not display anything). I also tried alerting the following:
$this->uri->uri_string()
and I get the same result (an empty alert box). What am I missing?
Edit: My controller is Pages.php:
<?php
class Pages extends CI_Controller {
public function view($page = 'home') {
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Upvotes: 4
Views: 1318
Reputation: 7615
As i have seen you are trying to apply current menu as class='active'. My sugesstion to you is that rather than finding uri and matching it. You can use following method to your controller and view.
Controller
$data['active_menu'] ='search'
or
$data['active_menu'] ='home'
or
$data['active_menu'] ='help'
`$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);`
View Pages (Home_view.php/search_view.php/help_view.php)
<a class="<?php if($active_menu=="search"){echo "active";}?>" href="<?=base_url('Pages')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
Upvotes: 2
Reputation: 38672
Try this
<a class="<?php if($this->uri->segment(1)=="Pages"){echo "active";}?>" href="<?=base_url('Pages')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
$this->uri->segment(1)
means www.example.com/Pages/my_Method
Base URL
= www.example.com
$this->uri->segment(1)
= Pages
$this->uri->segment(2)
= my_Method
Upvotes: 3