[英]When i use if else condition in query based on condition, how can i get that value on view in PHP CODEIGNITER
我對mysql中的這個問題感到困惑,我有兩個表“ A”和“ B”
TableA:
S.No contact1 contact2 status
1 Blbh eeee 1
TAbleB:
S.No Phone1 phone2
1 ddd ssss
從這個表中我將獲得價值,從表A中我將要檢查
if (status == 1)
{
run tableA;
}
else
{
run table b;
}
我不見了這個查詢的返回結果。 鑒於此,如何利用受尊重的列名獲取價值。 我對此一無所知,請幫助我獲得價值。
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()結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.