[英]How to pass Codeigniter controller array to view and use foreach function?
I can't access codeigniter controller array values in the view. 我无法在视图中访问codeigniter控制器数组值。 where did i miss. 我在哪里想念。
mymodel 我的模特
function get_data(){
$this->db->select('prod_id, content, details');
$query = $this->db->get('tblesample');
return $query->result();
}
controller 控制者
public function index(){
$this->load->model('mymodel');
$user_info = $this->mymodel->get_data();
$this->load->view('inc/header_view');
$this->load->view('data_view', $user_info);
}
data_view data_view
<div class="col-md-9">
<?php
foreach ($user_info as $info) {
echo $info->content;
}
?>
</div>
In controller change the code. 在控制器中更改代码。
public function index(){
$this->load->model('mymodel');
$data['user_info'] = $this->mymodel->get_data();
$this->load->view('inc/header_view');
$this->load->view('data_view', $data);
}
You are not passing the array correctly to view . 您没有将数组正确传递给view。 Pass your data like this 这样传递您的数据
public function index(){
$this->load->model('mymodel');
$user_info['user_info'] = $this->mymodel->get_data();
$this->load->view('inc/header_view');
$this->load->view('data_view', $user_info);
}
You be better off using associative array to pass on to the view: 您最好使用关联数组传递到视图:
public function index(){
$this->load->model('mymodel');
$data = [
'user_info' => $this->mymodel->get_data(),
];
$this->load->view('inc/header_view');
$this->load->view('data_view', $data);
}
And reference it like $user_info
in your view. 并在您的视图中像$user_info
一样引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.