Reputation: 77
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
Sample output and array values
while($row = mysql_fetch_array($sql))
{
$offerstatus[] = $row['offer_status'];
//some more output
}
Values stored in this array $offerstatus[] = enabled or disabled
these values are stored with reference to offer
Sample Values
offer status
offer1 enabled
offer2 enabled
offer3 disable
offern disable
or
condition offer status
50 51 enabled
100 99 enabled
122 865 disable
NNN offern disable
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.
On the basis of sample Values
The above should automatically be turned like this
if ($condition1 >= $offer1 AND $condition2 >= $offer2 )
{ //run code }
else { //some error messages }
Please let me know if the question is not well clarified.
Upvotes: 2
Views: 126
Reputation: 1064
You can try this :
/* 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';
}
Upvotes: 0
Reputation: 4808
condition and offer must be in array
$condition=array(50,100,122);
$offer=array(51,99,865);
Now filter the array those has value enabled
function filter_enabled($val){
if($val=='enabled'){
return true;
}
}
$filtered_offerstatus=array_filter($offerstatus,'filter_enabled');
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.
}
}
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";
}
Note: we assume that $condition ,$offer and $offerstatus have the same length of array, otherwise this program will not work.
Upvotes: 2
Reputation: 2037
You will want to not use the >=
operators. Swap it out for ===
. Your $condition variables will also need to be string values enable/disable. Then your evaluation IF bloc would be similar to this:
if ('enabled' === 'enabled' AND 'disabled' !== 'enabled') {
//run code
} else {
//some error messages
}
Upvotes: 1