2016-03-03 55 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