本文介绍了Codeigniter result_array()返回一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的表中有两行,但当我 print_r
$ data
它只返回db中的第二行为什么?
In my table I have two rows but when I print_r
the $data
that this model function is connected to it is only returning the second row in the db why?
模型功能:
function getAllUsers()
{
$query = $this->db->get('users');
foreach($query->result_array() as $row)
{
$row['id'];
$row['fName'];
$row['lName'];
$row['email'];
$row['password'];
}
return $row;
}
推荐答案
> $ row 是循环变量,它只保存循环退出后最后一次迭代的数据。
Because $row
is the loop variable, it will only hold the data from the last iteration after the loop has exited.
:
function getAllUsers()
{
$rows = array(); //will hold all results
$query = $this->db->get('users');
foreach($query->result_array() as $row)
{
$rows[] = $row; //add the fetched result to the result array;
}
return $rows; // returning rows, not row
}
strong>
In your controller:
$data['users'] = $this->yourModel->getAllUsers();
$this->load->view('yourView',$data);
在您的视图中
//in your view, $users is an array. Iterate over it
<?php foreach($users as $user) : ?>
<p> Your first name is <?= $user['fName'] ?> </p>
<?php endforeach; ?>
这篇关于Codeigniter result_array()返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!