当我在基于条件的查询中使用 if else 条件时,如何在 PHP CODEIGNITER 中查看该值

When i use if else condition in query based on condition, how can i get that value on view in PHP CODEIGNITER

我对 mysql 中的这个问题感到困惑,我有两个 table、"A" 和 "B"

TableA:
S.No    contact1 contact2  status
 1        Blbh    eeee       1

TAbleB:
S.No     Phone1   phone2
 1        ddd     ssss

我将从这个 table 中获得价值,我将从 TableA 中检查

if (status == 1)
{
  run tableA;
}
else
{
  run table b;
} 

我没有得到此查询的 return 结果。在视图中,如何使用受尊重的列名获取值。我不知道这个,帮助我获得价值。

public function contDetails($id){
        $check = $this->db->query("SELECT contact_status FROM account WHERE id = '$id' ");
        $str = $check->row();
        $chk = $str->contact_status;

        if($chk == 1){
            $query = $this->db->query("SELECT * FROM account WHERE id = '$id'");
        }else{
            $query = $this->db->query("SELECT * FROM contact_details WHERE user_id = '$id'");
        }

        $run = $query->num_rows();

        print_r($run);

    }

您可以在您的模型中使用

$query = $this->db->get(); //---  run the query  ---//
return $query->result() //---  to get result in array of object  ---//

然后在您看来使用 foreach 循环

foreach($results as $result){
 echo $result->columnName;
}

您是否已经在 mysql 中编写了查询?如果是,并且您担心要使用的列名是否存在,您可以使用 isset...

在您看来,您可以像下面这样使用:

     <?php

      foreach($results as $result)
      {
          echo (isset($result['col1']))?$result['col1']:$result['col1_2'];
      }


     ?>

并且不要忘记在控制器中使用 result_array() 代替结果