如何在codeigniter中创建带有活动类的if菜单

时间:2015-07-08 04:53:09

标签: php codeigniter if-statement frameworks

我想问一下如何使用活动类制作if else功能菜单。这是我在控制器中的代码。

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');
}

这是我的观点

<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-->

我会尝试使用activetabs。但是有一个错误。

2 个答案:

答案 0 :(得分:2)

将此更改为

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

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

如果您使用==,则会出现此错误

  

严重程度:通知
  消息:未定义的变量:activeTab
  文件名:xx / yyy.php
  行号:xxx

答案 1 :(得分:1)

您需要将$data数组传递给viwes==是否可以使用=来分配值

<强>控制器

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);
}