2

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.

Asif
  • 77
  • 1
  • 8

3 Answers3

2

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.

Dhairya Lakhera
  • 4,445
  • 3
  • 35
  • 62
  • Can you please explain this part of code ... foreach($filtered_offerstatus as $key=>$value){ if($condition[$key]>=$offer[$key]){ I am not able to understnad $key=>$value and if($condition[$key]>=$offer[$key]){ ------ Where will they get these values – Asif Mar 03 '16 at 18:05
  • I must say, you have given a perfect solution . if it worked well – Asif Mar 03 '16 at 18:06
  • check this manual to understand foreach [foreach](http://php.net/manual/en/control-structures.foreach.php) – Dhairya Lakhera Mar 03 '16 at 18:09
  • Note that $condition ,$offer and $offerstatus have same length of array. means if $condition have 14 value then $offer and $offerstatus must have 14 values. Otherwise this program will not work – Dhairya Lakhera Mar 03 '16 at 18:11
  • this is true, all of them have equal lenth – Asif Mar 03 '16 at 18:13
  • Ok if you find my answer suitable and correct, then vote for it community and accept the answer. – Dhairya Lakhera Mar 03 '16 at 18:15
  • for your last part ///if($check===true){ echo "Execute Code"; How will I execute my code .. How will I know which conditions have been true and which code to run now . as this will change with status=enabled or disable $condition1 >= $offer1 AND $condition2 >= $offer2 and so on . – Asif Mar 03 '16 at 18:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105294/discussion-between-curious-and-asif). – Dhairya Lakhera Mar 03 '16 at 18:15
  • I am waiting for your in chat room – Asif Mar 03 '16 at 18:20
1

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 
}
David J Eddy
  • 1,999
  • 1
  • 19
  • 37
0

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

}
Benjamin Poignant
  • 1,066
  • 8
  • 18