Welcome Guest, Not a member yet? Register   Sign In
Current page and 'active' class in menu
#1

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?
Reply
#2

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

(This post was last modified: 06-07-2015, 02:54 PM by gadelat.)

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
Reply
#4

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.
Reply
#5

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.
NexoPOS 2.6.2 available on CodeCanyon.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB