2016-09-27 36 views
0

下午好,所以我想檢查我的數據庫中的特定值在CakePHP中是否爲真。
我的SQL表稱爲口腔看起來像這樣CakePHP檢查數據庫中的特定值是否爲真

--------------------- 
    |PK|OralPresentation| 
    --------------------- 
    |1 |1    | 
    --------------------- 

所以,我想在我的控制做的是檢查,如果該值是OralPresentation是1
我在下面的代碼這樣做

  $OralStatus = $this->Submission->query("SELECT * FROM Oral;"); 
      //If Oral Presentations are activated 
      if($OralStatus['0']['PK']['OralPresentation']==1) 
      { 
       $this->set('Istrue','true'); //Passing this information to the view. 
      } 
      else 
      { 
       $this->set('Istrue','false'); 
      } 
     } 

現在,當我在我看來呼應IsTrue運算它總是儘管數據庫表爲1。另一種解決方法我都試過返回false是在IF語句

if($OralStatus['0']['PK']['OralPresentation']) 

回答

0

如果你知道你想檢查的PK,一個方便的方法是使用field

$this->Oral->id = 1; 
$Istrue = $this->Oral->field('OralPresentation'); // value for pk 1 
$this->set(compact('Istrue')); 

cake docs on field

php compact

+0

所以我會在IF語句上面插入代碼? 第一行陳述位置 第二行我檢查第二行 位置1處的Oralpresentation,我瞭解其他所有內容,但是Compact有什麼作用? –

+0

Compact創建一個包含變量及其值的數組。所以你可以用'$ this-> set(compact('Istrue'));''來爲你的視圖設置'$ Istrue'的值。或者,你可以用'$ this-> set('Istrue',$ Istrue);'替換if語句。 – bill

+0

我可能不得不保留IF,因爲如果它是真的,我想在視圖中創建一個鏈接。這就是爲什麼我通過價值 感謝您的幫助! –

0
 $statusQuery = $this->Submission->query("SELECT * FROM oral WHERE PK = 1"); 
     $this->set('status',$statusQuery['0']['oral']['OralPresentation']); 
     $status = $statusQuery['0']['oral']['OralPresentation']; 
     if($status['0']['oral']['OralPresentation']==1) 
     { 
      $this->set('Istrue',$status); 
     } 
     else 
     { 
      $this->set('Istrue',$status); 
     } 
    } 

試圖基於其他工作了幾個解決方案之後,我已經有了這個工作。它能夠將數據庫的值傳遞給視圖

相關問題