【问题标题】:How to make if else menu with active class in codeigniter如何在codeigniter中使用活动类制作if else菜单
【发布时间】:2015-07-08 04:53:09
【问题描述】:

我想问如何使用活动类制作 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。但是有一个错误。

【问题讨论】:

  • 你在任何地方定义$activeTab吗?
  • @Bankzilla 我已经在任何地方定义了,还是一样
  • but error. 你能解释一下你遇到了什么错误吗??
  • @Saty 错误未定义变量:数据和未定义变量:activeTab
  • What is your view name and paste you full error with line number

标签: php codeigniter if-statement frameworks


【解决方案1】:

把这个改成

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

这个

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

如果你使用==,你会得到这个错误

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

【讨论】:

【解决方案2】:

您需要将$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);
}

【讨论】:

  • ok print_r($activeTab); 在你的 viwe 中检查它返回的内容
  • 此错误严重性:通知消息:未定义变量:activeTab 文件名:views/content-about.php 行号:5 回溯:文件:C:\xampp\htdocs\codeig\application\views\ content-about.php 行:5 功能:_error_handler 文件:C:\xampp\htdocs\codeig\application\controllers\Site.php 行:43 功能:查看 文件:C:\xampp\htdocs\codeig\index.php 行: 292 功能:require_once
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2022-06-13
  • 1970-01-01
相关资源
最近更新 更多