根据codeigniter中的条件获取结果

时间:2019-02-11 08:55:34

标签: php 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
)

5 个答案:

答案 0 :(得分:0)

     foreach ($rows as $rown)
        {

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

答案 1 :(得分:0)

您正在循环内替换数组$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,然后将其添加到数组$newEntry

答案 2 :(得分:0)

尝试一下

        $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;

请先阅读评论部分

答案 3 :(得分:0)

在上述模型查询中,将$ queryc更改为$ query,如

{{1}}

答案 4 :(得分:-1)

将循环更改为此,希望它对您有用。

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