[英]I can't pass the data from the controller to the view in CodeIgniter 3
I can't pass the data from the controller to the view in CodeIgniter 3. All the thing seem okay.我无法将 controller 中的数据传递到 CodeIgniter 3 中的视图。一切看起来都还不错。 Do I need to put the Controller's part in the index() method?
我需要将控制器的部分放在 index() 方法中吗?
Donor_Model.php Donor_Model.php
function viewDonors() {
$query = $this->db->get('donors');
return $query;
}
Staff.php controller员工.php controller
public function viewDonors()
{
$this->load->Model('Donor_Model');
$data['donors'] = $this->Donor_Model->viewDonors();
$this->load->view('viewdonors', $data);
}
When I try to call the $data in the viewdonors.php view, it shows the $donors as undefined.当我尝试在 viewdonors.php 视图中调用 $data 时,它会将 $donors 显示为未定义。
@TimBrownlaw is right. @TimBrownlaw 是对的。 Even though the IDE shows an error, the code runs without a problem.
即使 IDE 显示错误,代码运行也没有问题。
You need to edit your model page to:-您需要将 model 页面编辑为:-
function viewDonors() {
$query = $this->db->get('donors');
return $query->result();
}
As you want to loop through donors data in the view page so you need to return result function in the model.由于您想在视图页面中循环浏览捐助者数据,因此您需要在 model 中返回结果 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.