2016-03-03 54 views
2

我想运行基于代码或从php数组返回的值。如果返回值被禁用,则禁用状态的报价将不匹配。有条件地运行php代码

这行代码工作正常。

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 []返回的值运行上面的查询是.so,只有那些条件相匹配,其值被启用。
问题:
我想运行这个代码的所有值被重新调整为启用,并希望匹配这些条件。

在样本的基础价值观

以上应自动开启这样

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

请让我知道,如果这个问题没有得到很好的阐明。

+0

最多可以提供多少优惠?并提供包含(1和0)或(启用和禁用)? – Curious

+0

检查我的答案,并批准它,如果这有助于你 –

+0

@Curious,将会有最优惠的14个优惠。 – Asif

回答

2

条件和报价必须在阵列

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

现在筛选阵列那些具有价值启用

function filter_enabled($val){ 
    if($val=='enabled'){ 
     return true; 
    } 
} 

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

现在$filtered_offerstatus只包含那些值为启用,现在检查条件大于等于提供

$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. 
     } 
} 

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

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

注:我们假设$条件,$ offer和$ offerstatus具有相同的数组长度,否则这个程序将不起作用。

+0

您能否请您解释这部分代码... foreach($ filtered_offerstatus as $ key => $ value){ if($ condition [$ key]> = $ offer [$ ($ key)> { 我不能够忽略$ key => $ value,并且if($ condition [$ key]> = $ offer [$ key]){------他们在哪里得到这些值 – Asif

+0

我必须说,你已经给出了一个完美的解决方案。如果运行良好 – Asif

+0

请查看本手册以了解foreach [foreach](http://php.net/manual/en/control-structures.foreach.php) – Curious

0

你可以试试这个:

/* 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'; 

} 
+0

,可否请您给代码写一些解释。 – Asif

+0

当然我可以,我已经编辑了一些解释我的回答,因为我在评论如果你不明白的东西 –

1

你想不使用>=运营商。把它换成===。您的$条件变量也需要是字符串值的启用/禁用。那么你的评估如果集团将与此类似:

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

我认为$ conditionn和$ offern是另一个值,检查我的回答 –

+0

,条件将只是> =,它不能是==,只有在$ condition1> = $ offer1的情况下,代码才会生效。等等 – Asif

+0

$ condition1中的值是什么? – Curious