在我的表中我有两行,但是当我print_r
此模型函数连接到它的$data
时,它只返回db中的第二行为什么?
模型功能:
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;
}
答案 0 :(得分:4)
因为$row
是循环变量,所以它只会在循环退出后保存最后一次迭代的数据。
这样做:
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
}
在你的控制器中:
$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; ?>