如何在codeigniter中显示所有值


How to display all values in codeigniter

我正在使用以下代码,但只有一个结果。

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);
    }
}

如何显示数据库中的所有值?。请帮帮我。

使用$query->result()方法object返回使用result_array返回数组中的值

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

使用result_array

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

您只打印一个值。

你需要获取数组中的所有值并打印出来

更正代码:

<?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()

此方法以纯数组或空数组的形式返回查询结果当没有生成结果时使用数组。通常,您将在foreach循环,如下所示:

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

您应该使用如下

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