CodeIgniter Forums

Full Version: Current page and 'active' class in menu
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

I have a problem, I want set 'active' class to navbar on current page. I tried to something like this but it doesn't work:
PHP Code:
<?=($this->uri->segment(1)==='main/index')?'active':''?>

I have all pages in one controller:
PHP Code:
class Main extends CI_Controller {

    function 
__construct() {
        
parent::__construct();
    }

    public function 
index() {
        
$data['title'] = 'Aktualnoƛci';
        
$this->load->view('templates/web/header'$data);
        
$this->load->view('vrs-pages/index');
        
$this->load->view('templates/web/footer');
    }

    public function 
page_InformacjeTurniej() {
        
$data['title'] = 'O turnieju';

        
$this->load->view('templates/web/header'$data);
        
$this->load->view('vrs-pages/informacje/o-turnieju');
        
$this->load->view('templates/web/footer');
    }

    public function 
page_InformacjeNagrody() {
        
$data['title'] = 'Nagrody';
        
        
$this->load->view('templates/web/header'$data);
        
$this->load->view('vrs-pages/informacje/nagrody');
        
$this->load->view('templates/web/footer');
    }

        
// ETC ...


How can I do this?
Change segment(1) to segment(2).
Correct me if I am wrong.
I'm new to CI 3.0.
uri->segment returns only one segment. Segments are split by slashes. So it can't be 'main/index'. One segment is either 'main' or 'index'. If you have all stuff in one controller, you don't really need controller check, so use just the second segment, like
PHP Code:
<?=($this->uri->segment(2)==='index')?'active':''?>

You can also use router->fetch_method instead, which is more reliable as it is less prone to errors caused by changes in urls, routing config and/or .htaccess
You could use a constructor variable (array) to store the names of all pages in your menu. Then pass that variable along with the active pagename from the controller method to the view file. Then in the viewfile create a loop for the array and check inside the loop if that pagename is the same as the current pagename and fill your navbar.
Hi,

Uri->segment return only the firts segment. You should create specific function with can make a comparison with all Uri parameters. Check doc to know which method returns Uri segment.