簡體   English   中英

如何在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);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM