我的控制器没有传递$data
给我的视图,我不知道为什么不传递。我正在重用以前项目中运行良好的代码,并且我当然理解 $data 传递是如何工作的想法。但也许我在复制代码时错过了什么?
我把变量$data['hello']
放在那里只是为了测试目的。正如您从输出中看到的$hello
那样,甚至没有通过。if
失败并且else
代码正确运行,这意味着正在加载视图文件本身。
控制器:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
看法:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
输出(删节):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
编辑:应要求提供更多信息:型号:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}