简体   繁体   English

如何在codeigniter中显示所有值

[英]How to display all values in codeigniter

I am using following code but only one result will come. 我正在使用以下代码,但只会有一个结果。

function group($id)
{
    $this->db->select('groupId,groupName');    
    $this->db->from('groups');
    $this->db->where('createdBy',$id);
    $query = $this->db->get();
    foreach($result=$query->row_array() as $row)
    {
        print_r($result);
    }
}

How can I display all values from database?.Please help me. 如何显示数据库中的所有值?。请帮助我。

Use $query->result() method object returns use result_array for returning value in array 使用$query->result()方法object返回使用result_array返回数组中的值

foreach ($query->result() as $row)
{
    echo $row->groupId;//column names
}

To use result_array 要使用result_array

foreach ($query->result_array() as $row)
{
    echo $row['groupId'];//column names
}

You are printing only one value. 您只打印一个值。

You need to take all values in an array and print it. 您需要获取数组中的所有值并进行打印。

Corrected code: 更正代码:

<?php
function group($id) {
    $this->db->select('groupId,groupName');
    $this->db->from('groups');
    $this->db->where('createdBy', $id);
    $query = $this->db->get();
    $arr = array();
    foreach ($query->row_array() as $row) {
        $arr[] = $row;
    }
    print_r($arr);
}
?>

result_array() result_array()

This method returns the query result as a pure array, or an empty array when no result is produced. 此方法将查询结果作为纯数组返回,或者在未生成结果时返回空数组。 Typically you'll use this in a foreach loop, like this: 通常你会在foreach循环中使用它,如下所示:

foreach($query->row_array() as $row)
    {
        echo $row['groupId'];
        echo $row['groupName'];

    }

you should use as below 你应该使用如下

foreach($query->row_array() as $row)
 {
    print_r($row);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM