簡體   English   中英

根據codeigniter中的if條件獲取結果

[英]Get result according to if condition in codeigniter

我正在使用以下代碼,如果狀態為“0”,則應顯示“已批准”,如果狀態為“1”,則應顯示不批准,我該怎么做,我嘗試使用以下代碼但對我不起作用

$this->db->select('*');
$this->db->from('surgery');
$this->db->where('doctor_id',$add_data['doctor_id']);
$query = $this->db->get();

if ( $queryc->num_rows() > 0 )
{
    $rows = $query->result_array();
    $newEntry=array();
    foreach ($rows as $rown)
    {
        $newEntry = $rown;
        if($rown['status']=="0")
        {
            $newEntry["status"] = "dissapproved";
        }
        else
        {
            $newEntry["status"] = "approved";
        }
    }
}

上面的代碼顯示一個結果/記錄,就像下面這樣

Array
(
    [id] => 5
    [status] => dissapproved
)
     foreach ($rows as $rown)
        {

            if($rown['status']=="0")
            {
               $rown['status'] = "dissapproved"; 
            }
            else
            {
               $rown['status'] = "approved";
            }
           $newEntry =$rown;
        }

您正在循環內替換數組$newEntry 這就是為什么你只會得到一個結果。 試試下面的代碼。

 if ($query->num_rows() > 0) {
    $rows = $query->result_array();
    $newEntry = array();
    foreach ($rows as $rown) {
        if ($rown['status'] == "0") {
            $rown["status"] = "dissapproved";
        } else {
            $rown["status"] = "approved";
        }
        $newEntry[] = $rown;
    }
}

只需將$rown['status']替換為新字符串approved / dissapproved approved然后將其添加到您的數組$newEntry

嘗試這個,

        $i=0;
        foreach ($rows as $col) {
            //below code if you use $query->result();
            if ($col->status == 0) {
                $col->status = "dissapproved";
            } else {
                $col->status = "approved";
            }
            //below code if you use $query->result_array();
            if ($col['status'] == 0) {
                $rows[$i]['status'] = "dissapproved";
            } else {
                $rows[$i]['status'] = "approved";
            }
            $i++;
        }
        echo'<pre>';print_r($rows);die;

請先閱讀評論部分

在上面的模型查詢中,將 $queryc 更改為 $query 之類的

if ( $queryc->num_rows() > 0 ) Instead if ( $query->num_rows() > 0 )

將您的循環更改為此,希望它對您有用。

foreach ($rows as $rown)
            {
                $newEntry = $rown;
                if($rown['status']=="0")
                {
                    $newEntry["status"][] = "dissapproved";
                }
                else
                {
                    $newEntry["status"][] = "approved";
                }
            }

暫無
暫無

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

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