【问题标题】:CodeIgniter Query Results Only Displays Last Row in ViewCodeIgniter 查询结果仅在视图中显示最后一行
【发布时间】:2013-09-16 00:10:14
【问题描述】:

我想显示我的数据库中的结果列表。目前,我的视图只显示我的查询检索到的最后一行。我在这里想念什么?感谢您提供的任何帮助。

型号:

public function get_agencies() {
    $this->db->select("AgencyNumber, AgencyName, users.id, active");
    $this->db->from('Agency, users');
    $this->db->where('users.id = Agency.id');
    $q = $this->db->get();

    if($q->num_rows() > 0) {
        foreach($q->result() as $agency) {
            $data['agencies'] = $agency;
        }
        return $data;
    }
}

控制器:

function modify_agency() {
    $this->load->model('ion_auth_model');
    $this->data['agencies'] = $this->ion_auth_model->get_agencies();

    //added the following 2 lines to load view with header and footer from template         
    $this->data['main_content'] = 'auth/modify_agency';
    $this->load->view('./includes/template', $this->data);
}

查看:

<?php foreach ($agencies as $agency):?>
    <tr>
        <td><?php echo $agency->AgencyNumber;?></td>
        <td><?php echo $agency->AgencyName;?></td>
        <td><?php if($agency->active == 1) { echo 'Active'; } else { echo 'Inactive'; };?></td>
    </tr>
<?php endforeach;?>

【问题讨论】:

    标签: php mysql codeigniter templates foreach


    【解决方案1】:

    应该是这样的。

    $data[] = $agency;
    

    您不需要解析值机构。CodeIgniter 为您完成

    $data['agencies'] = $agency;
    

    试试看。

    【讨论】:

    • 太棒了!我将我的模型更新为$data[] = $agency;,现在我的完整结果正在显示。感谢您的帮助!
    【解决方案2】:

    在您的模型中,您不会将 $agency 变量推入数组中。它们在每次迭代中都会被替换,因此$data['agencies'] 将只包含最后一次迭代的值。此外,正如 Syed 上面回答的那样,您不需要在代码中包含数组索引值

    改成:

    $data[] = $agency;
    

    或:

    array_push($data, $agency);
    

    希望这会有所帮助!

    【讨论】:

    • 这是有道理的,但仍有问题。在我的模型中,我将$data['agencies'] = $agency; 更新为$data['agencies'][] = $agency;。现在从我的角度接收 PHP 错误 - “尝试获取非对象的属性”。
    • @user1337595:很高兴知道你让它工作了。无论如何,为了正确起见,我已经更新了这个答案。干杯! :)
    • 好的,让我运行你的逻辑,你有用户和关于用户的数据,你想显示特定用户的数据不是吗
    • @syedshah:嗯?您的回答已被接受。另外,我认为您在评论错误的线程;)
    • 是的,对不起@AmalMurali
    【解决方案3】:

    控制器:

    (...)
    $this->data['agencies'] = $this->ion_auth_model->get_agencies();
    (...)
    $this->load->view('./includes/template', $this->data);
    (...)
    

    型号:

    (...)
    if($q->num_rows() > 0) {
        foreach($q->result() as $agency) {
            $data['agencies'] = $agency;
        }
        return $data;
    }
    

    查看:

    <?php foreach ($agencies as $agency):?>
    (...)
    

    请注意,如果您的 get_agencies 中没有一行结果,则您不会返回任何内容,并且您将在视图中的 foreach 函数中收到错误。

    你可以这样返回:

    public function get_agencies() {
        $this->db->select("AgencyNumber, AgencyName, users.id, active");
        $this->db->from('Agency, users');
        $this->db->where('users.id = Agency.id');
        $q = $this->db->get();
    
        return ($q->num_rows() > 0) ? $q->result() : array();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多