Php 已传递变量,但仍引发异常?
我创建并传递一个数据数组:Php 已传递变量,但仍引发异常?,php,codeigniter,Php,Codeigniter,我创建并传递一个数据数组: public function home() { $data['page'] = 'home'; $data['table'] = 'pageData'; $data['temp'] = 'temp_1'; $this->template($data); } public function template($data) { $this->load->model("model_get")
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
$this->load->view('template', $data);
}
这是模板视图:
<?php
$this->load->view('header');
$this->load->view('nav', $data);
$data['results'] = $results;
$this->load->view($temp, $data);
$this->load->view('footer');
?>
但是仍然加载视图并完成其中的所有if语句,并从存储在$temp
中的名称加载视图
为什么会抛出异常?您没有在该视图中填充$data以加载第二个视图。您的代码名称太模糊,无法真正理解它应该做什么,但让我们将其分解
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
//I assume getData takes a table name and returns all the data
//This only works because you're taking all the data passed from the first function
//in the function above.
$this->load->view('template', $data);
}
<?php
$this->load->view('header');
$this->load->view('nav', $data);
//At this point $data is empty. You have available $results, $page, $table and $temp
//because they were passed from the template function above.
$this->load->view('nav', $results);
//The line above is what it appears you need, rather than $data.
$data['results'] = $results;
//You've repopulated $data here but now all it contains is $results.
$this->load->view($temp, $data);
$this->load->view('footer');
?>
公共功能主页()
{
$data['page']='home';
$data['table']='pageData';
$data['temp']='temp_1';
$this->template($data);
}
公共功能模板($data)
{
$this->load->model(“model_get”);
$data['results']=$this->model\u get->getData($data);
//我假设getData使用一个表名并返回所有数据
//这只适用于获取从第一个函数传递的所有数据
//在上面的函数中。
$this->load->view('template',$data);
}
显然,您需要检查数据变量并查看它返回什么。使用print\r该豁免声明其未定义。它所做的只是回显它未定义,但它使用了变量,在xamp的一侧,它工作正常,我单独添加了导航菜单,它在错误后不精确,但直到错误发生,这已关闭服务器,就像我说的,它仍然加载内容,我认为这是变量的作用域问题。我如何纠正作用域问题,重新定义它?
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
//I assume getData takes a table name and returns all the data
//This only works because you're taking all the data passed from the first function
//in the function above.
$this->load->view('template', $data);
}
<?php
$this->load->view('header');
$this->load->view('nav', $data);
//At this point $data is empty. You have available $results, $page, $table and $temp
//because they were passed from the template function above.
$this->load->view('nav', $results);
//The line above is what it appears you need, rather than $data.
$data['results'] = $results;
//You've repopulated $data here but now all it contains is $results.
$this->load->view($temp, $data);
$this->load->view('footer');
?>