简体   繁体   中英

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

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

To use 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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