CodeIgniter Forums
Current page and 'active' class in menu - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Current page and 'active' class in menu (/showthread.php?tid=62075)



Current page and 'active' class in menu - vertisan - 06-07-2015

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?


RE: Current page and 'active' class in menu - JJARMANI - 06-07-2015

Change segment(1) to segment(2).
Correct me if I am wrong.
I'm new to CI 3.0.


RE: Current page and 'active' class in menu - gadelat - 06-07-2015

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


RE: Current page and 'active' class in menu - Diederik - 06-07-2015

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.


RE: Current page and 'active' class in menu - Blair2004 - 06-07-2015

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.