2016-09-27 112 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); 
     } 
    } 

试图基于其他工作了几个解决方案之后,我已经有了这个工作。它能够将数据库的值传递给视图

相关问题