这是我的 html 代码,我是 codeigniter 的新手,我不知道我在代码中犯了什么错误。这是一个从表中获取记录的简单查询。请帮助我。
<?php foreach($records as $r):?>
<tr>
<td><?=$r['id']?></td>
<td><?=$r['name']?></td>
</tr>
<?php endforeach;?>
我的 Controller 代码
public function index(){
/*$this->load->view('student/student_view');*/
$data['records']=$this->student_model->getAll();
$data['student']=0;
$this->load->view('student/student_view',$data);
}
和我的模态代码
function getAll(){
$query = $this->db->get('stud');
return $query->result();
}
最佳答案
您正在从模态返回一个像数据这样的对象,并且您正在以数组的形式获取该数据,因此您需要将任一模态更改为
function getAll(){
$query = $this->db->get('stud');
return $query->result_array();
}
使用相同的 View ,现在 html 代码没有变化。 别的 为模态保留相同的代码
function getAll(){
$query = $this->db->get('stud');
return $query->result();
}
更改 html View 代码
<?php foreach($records as $r):?>
<tr>
<td><?=$r->id?></td>
<td><?=$r->name?></td>
</tr>
<?php endforeach;?>
通常我们使用 -> 来表示对象,使用 [] 来获取数组记录。希望这会起作用。祝你好运。
关于javascript - 无法在codeigniter中的foreach中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914449/