我无法访问视图中的codeigniter控制器数组值。我错过了什么
mymodel
function get_data(){
$this->db->select('prod_id, content, details');
$query = $this->db->get('tblesample');
return $query->result();
}
控制器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
<div class="col-md-9">
<?php
foreach ($user_info as $info) {
echo $info->content;
}
?>
</div>
在控制器中更改代码。
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);
}
你最好使用关联数组传递给视图:
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);
}
并在您的视图中将其引用为$user_info
没有正确地将数组传递给view。像这样传递你的数据
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);
}