0

我无法在视图页面中传递变量

这是我的控制器代码

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Management extends CI_Controller {

        public function __construct()
       {
            parent::__construct();
            if($this->session->userdata('level') != 1){
                redirect('');
            } 
        }


        public function hotels()
        {   

                $this->load->model('model_hotels');

                $ss['most_view_hotels'] = '23';
                $this->load->view("management/header_mng_view");
                $this->load->view('management/hotels_mng_view' , $ss , true);
                $this->load->view("management/footer_mng_view");
        }

}

这是错误

遇到 PHP 错误

严重性:通知

消息:未定义变量:most_view_hotels

文件名:管理/hotels_mng_view.php

行号:22

hotels_mng_view.php

<?php 
  echo $most_view_hotels;
  foreach($most_view_hotels as $value):
?>
  <div class="row">
  <div class="cell en">adsf</div>
  <div class="cell en">1212</div>
  <div class="cell en">12</div>
  <div class="cell en">32</div>
  </div>
<?php endforeach;?>
4

4 回答 4

1

在您的控制器中:

$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view',$ss);
$this->load->view("management/footer_mng_view");

在您看来:

<?php echo $most_view_hotels?>
于 2012-11-01T12:14:29.867 回答
0

在您的模板中,您会将项目称为...

$most_view_hotels

所以我们需要验证这是你在视图中所做的management/hotels_mng_view.php

于 2012-11-01T12:18:59.693 回答
0

如 Codeigniter 手册所述:

There is a third optional parameter lets you change the behavior of the function
so that it returns data as a string rather than sending it to your browser.
This can be useful if you want to process the data in some way. If you set 
the parameter to true (boolean) it will return data. 
The default behavior is false, which sends it to your browser.

这意味着您的线路

$this->load->view('management/hotels_mng_view' , $ss , true);

实际上将该视图的结果作为字符串返回给 php,而不是将其发送到浏览器。

您的实际问题(未定义变量)的答案依赖于 in management/hotels_mng_view.php,可能是错字。

于 2012-11-01T12:49:14.597 回答
0

我认为问题出在第三个参数上,即true.

正如 codeigniter 文档所说:

There is a third optional parameter lets ..... 
Remember to assign it to a variable if you want the data returned

并且在您的代码中,您没有将返回的数据分配给任何代码。我建议你尝试这样的事情:

public function hotels()
        {   

           $this->load->model('model_hotels');
           $data = array();
           $ss['most_view_hotels'] = '23';
           $data['header'] = $this->load->view("management/header_mng_view",,true);
           $data['body'] = $this->load->view('management/hotels_mng_view' , $ss , true);
           $data['footer'] = $this->load->view("management/footer_mng_view");
           $this->load->view("template",$data);
        }

 /*template view file*/
 <?php
    echo $header;
    echo $body;
    echo $footer;
 ?>

希望这可以帮助。

于 2012-11-02T04:31:57.953 回答