简体   繁体   中英

How to make if else menu with active class in codeigniter

I want to ask how to make if else function menu with active class. here is my code in controller.

public function index(){

    //i try with this but error 
    $data['activeTab'] == "home";

    $this->load->view('head');
    $this->load->view('header');

    $this->load->view('content',$data);

    $this->load->view('footer');
    $this->load->view('foot');
}

public function about(){

    //i try with this but error
    $data['activeTab'] == "about";

    $this->load->view('head');
    $this->load->view('header');

    $this->load->view('content-about',$data);

    $this->load->view('footer');
    $this->load->view('foot');
}

and here is my view

<body>  
<div class="container">
    <div class="row">
        <!--HEADER START-->
            <div class="page-header">
            <a href="<?php echo base_url();?>codeig/index.php/site">HEADER</a>
            </div>
        <!--HEADER END-->

        <!--NAVBAR START-->
            <nav class="navbar navbar-default">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                </div>
                <div id="navbar" class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <!--i try with this but error-->
                        <li class="<?php echo ($activeTab=="home")?"active":""; ?>"><a href="<?php echo base_url();?>codeig/index.php/site">Home</a></li>
                        <li><a href="<?php echo base_url();?>codeig/index.php/site/about">About</a></li>
                        <li><a href="#contact">Contact</a></li>
                    </ul>
                </div>
            </nav>
        <!--NAVBAR END-->

I'll try with activetabs . But there is an error.

Change this to

$data['activeTab'] == "home";
$data['activeTab'] == "about";

this

$data['activeTab'] = "home";
$data['activeTab'] = "about";

If you use == , You get this Error

Severity: Notice
Message: Undefined variable: activeTab
Filename: xx/yyy.php
Line Number: xxx

You need to pass $data array to your viwes ANd == is your for compair use = for assign value

Controller

public function index(){

    //i try with this but error 
    $data['activeTab'] = "home";

    $this->load->view('head',$data);
    $this->load->view('header',$data);

    $this->load->view('content',$data);

    $this->load->view('footer',$data);
    $this->load->view('foot',$data);
}

public function about(){

    //i try with this but error
    $data['activeTab'] = "about";

    $this->load->view('head',$data);
     $this->load->view('header',$data);

    $this->load->view('content-about',$data);

    $this->load->view('footer',$data);
    $this->load->view('foot',$data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM