如何在条件符合php

时间:2017-02-07 10:45:13

标签: php cakephp

id,name,status
1   a    3
2   b    3
3   c    3

我的桌子上有多行。

当我点击提交按钮时,如果所有行的状态为4,则检查所有行,然后我将保存条件设置为其他代码。

我需要计算我的表的行数,假设它们是2行,状态== 3然后我的forloop计数行,如果所有2行都是status == 4则创建保存逻辑..

请帮我实施

下面是我的代码..但它对我不起作用..

$cond = array('OrderDetail.order_id'=>trim($this->requestData['orderId'])); 
$orderData = $this->OrderDetail->find('all',array('conditions'=>$cond));
$numData = sizeof($orderData);
$count=1;
foreach ($orderData as $value) {
    if($value['OrderDetail']['status'] > 3 && $value['OrderDetail']['status']!=5){
        if($numData == $count) {
            // if condition meets and all the rows of table have status==4 then 
            // save logic here
            // }
        }
        $count= $count+1;
    } 

1 个答案:

答案 0 :(得分:0)

你可以这样做:

 $cond = array('OrderDetail.order_id'=>trim($this->requestData['orderId'])); 
 // condition for status not equal to 4
 $conditionForStatus = array(
                       'OrderDetail.status !='=>4,
                       // 'OrderDetail.order_id'=>trim($this->requestData['orderId'])
                       );

 $orderData = $this->OrderDetail->find('all',array('conditions'=>$cond));

 // find count of record where status is not equal to 4
 $countOfStatus = $this->OrderDetail->find('count',array('conditions'=>$conditionForStatus ));
 if(!empty($orderData) && $countOfStatus == 0) {
    // your save logic here
 }

你只需要表中的一些记录,但只有状态4。我是对的吗?