【问题标题】:Run php code conditionally有条件地运行php代码
【发布时间】:2016-06-17 01:15:59
【问题描述】:

我想根据 php 数组返回的值或值运行代码。如果 value 返回为 disabled ,则将不匹配处于 disabled 状态的 offer。

这行代码运行良好。

if ($condition1 >= $offer1 AND $condition2 >= $offer2  AND 
$condition3 >= $offer3  AND  $condition4 >= $offer4     ) 
      {  //run code }
else {  //some error messages  }


$condition1 and all are numeric value 
$offer1  and all are numeric value

样本输出和数组值

while($row = mysql_fetch_array($sql))  
{
$offerstatus[] = $row['offer_status'];
//some more output
}

存储在此数组中的值 $offerstatus[] = 启用或禁用

这些值是参考报价存储的
样本值

offer   status 
offer1  enabled
offer2  enabled 
offer3  disable
offern  disable

condition     offer status
 50           51    enabled
100           99    enabled
122          865    disable
NNN          offern disable

我想根据从这个数组返回的值运行上述查询 $offerstatus[] 。这样只有那些值被启用的条件才匹配。
问题:
我想为所有返回为 enabled 的值运行此代码,并希望匹配这些条件。

基于样本值

上面应该自动变成这样

if ($condition1 >= $offer1 AND $condition2 >= $offer2     ) 
      {  //run code }
else {  //some error messages  }

如果问题没有得到很好的澄清,请告诉我。

【问题讨论】:

  • 最多可以提供多少个报价?并提供包含(1和0)或(启用和禁用)?
  • 查看我的回答,如果对您有帮助,请批准
  • @Curious , 最多有 14 个优惠。
  • 您还没有澄清问题。首先你说 $offer1 都是数值。然后你说 $offer1 包含启用和禁用。您还必须让我们知道该条件变量包含什么值。
  • $offer1 不包含 enable 或 disable ,但 $offerstatus 是启用或禁用。请查看已编辑的问题

标签: php mysql sql arrays


【解决方案1】:

条件和报价必须在数组中

$condition=array(50,100,122);
$offer=array(51,99,865);

现在过滤具有 enabled

值的数组
function filter_enabled($val){
    if($val=='enabled'){
        return true;
    }
}

$filtered_offerstatus=array_filter($offerstatus,'filter_enabled');

现在$filtered_offerstatus 只包含那些启用的值,现在检查条件是否大于等于offer

$check=false;
foreach($filtered_offerstatus as $key=>$value){

        if($condition[$key]>=$offer[$key]){
            $check=true;
        }
        else{
            $check=false;
            break; //if $condition is less than $offer it will get out of loop.
        }
}

现在如果所有值设置为 true 代码将被执行,否则 错误消息

if($check===true){
    echo "Execute Code";
}
else{
    echo "Some Error Message";
}

注意:我们假设 $condition ,$offer 和 $offerstatus 的数组长度相同,否则这个程序将无法运行。

【讨论】:

  • 你能解释一下这部分代码吗... foreach($filtered_offerstatus as $key=>$value){ if($condition[$key]>=$offer[$key]){我无法理解 $key=>$value 和 if($condition[$key]>=$offer[$key]){ ------ 他们从哪里得到这些值
  • 我必须说,你给出了一个完美的解决方案。如果它运作良好
  • 查看本手册了解foreach foreach
  • 注意 $condition ,$offer 和 $offerstatus 的数组长度相同。表示如果 $condition 有 14 个值,那么 $offer 和 $offerstatus 必须有 14 个值。否则此程序将无法运行
  • 这是真的,它们都有相同的长度
【解决方案2】:

您将不想使用>= 运算符。换成===。您的 $condition 变量也需要是启用/禁用的字符串值。那么您的评估 IF bloc 将与此类似:

if ('enabled' === 'enabled'  AND 'disabled' !== 'enabled') {
    //run code
} else {
    //some error messages 
}

【讨论】:

  • 我认为 $conditionn 和 $offern 是另一个值,请查看我的答案
  • , 条件只有 >= 为真,不能为 == ., 代码只有在 $condition1 >= $offer1 时才会成立。等等
  • $condition1 的值是多少?
  • $condition1 和 $offer1,包含一些特定值的 COUNT(),如果一个计数高于其他计数,代码将运行。 $condition1 >= $offer1,
【解决方案3】:

你可以试试这个:

/* SAMPLE VALUE */
$offerstatus = array('1'=>'enabled','2'=>'enabled','3'=>'disable');//in your case from your bdd

$condition1 = 15;
$offer1 =10;
$condition2 = 15;
$offer2 =10;


$one_condition_error=false;//boolean to check if all condition is true

foreach($offerstatus as $offer => $status){//foreach offerstatus
    if($status=='enabled'){//only if this offerstatus is enabled
        if(${'condition'.$offer} < ${'offer'.$offer}){// i check if conditionN < offerN : ${'test_'.$i}  -> $test1 if $i =1
            $one_condition_error=true;//one error detected
        }

    }       
}

if($one_condition_error){
    echo 'at least one offerstatus error';
}else{
    echo 'all offerstatus ok';

}

【讨论】:

  • ,你能不能给代码写一些解释。
  • 当然可以,我已经用一些解释编辑了我的答案,如果你不明白的话,我在评论中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2014-04-30
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多