Codeigniter result_array() 返回一行 [英] Codeigniter result_array() returning one row

查看:23
本文介绍了Codeigniter result_array() 返回一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表中我有两行但是当我 print_r 这个模型函数连接到它的 $data 只返回数据库中的第二行为什么?

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
}

在您的控制器中:

$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() 返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆