有条件地运行php代码 [英] Run php code conditionally

查看:104
本文介绍了有条件地运行php代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于php数组返回的值或值来运行代码.如果将值返回为Disabled,则处于禁用状态的商品将不匹配.

I want to run the code on the basis or values returned from php array. If value is returned as disabled , offer with disabled status will not be matched.

这行代码可以正常工作.

This line of code is working fine .

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 []中的值=启用或禁用

Values stored in this array $offerstatus[] = enabled or disabled

这些值存储为参考offer
样本值

these values are stored with reference to offer
Sample Values

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的所有值运行此代码,并希望匹配这些条件.

I want to run the above query based on values returned from this array $offerstatus[] .so that only those conditions are matched whose values are enabled.
Question:
I want to run this code for all the values that are retunred as enabled , and want to match those conditions.

基于样本值

以上内容应自动变为

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

如果问题不清楚,请告诉我.

Please let me know if the question is not well clarified.

推荐答案

条件和要约必须位于数组中

condition and offer must be in array

$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仅包含启用的那些值,现在检查条件是否大于要约

Now $filtered_offerstatus contains only those value which are enabled , now check if condition is greater than equal to 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 ,则将执行该代码,否则将执行错误消息

Now if all value set to true the code will be executed otherwise error message

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

注意:我们假设$ condition,$ offer和$ offerstatus具有相同的数组长度,否则该程序将无法工作.

Note: we assume that $condition ,$offer and $offerstatus have the same length of array, otherwise this program will not work.

这篇关于有条件地运行php代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆