-4

I have already asked this question here. Run php code conditionally

I have made some modification to the db. The answer given before might be correct , I have tried them but does not work. Those answers might be above my PHP understanding.

Below is rephrased version of the same question.

The actual code that i want to replace

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

I want to replace this code with the new one. sample below.

if ($offercurrent[0]>=$offerpoints[0] AND $offercurrent[1]>=$offerpoints[1]  AND 
$offercurrent[2]>=$offerpoints[2] AND  $offercurrent[3]>=$offerpoints[3]     ) 
      {  //run code }
else {  //some error messages  }

$offerstatus = enabled or disabled
$offercurrent = numeric values
$offerpoints = numeric values


$offercurrent   $offerpoints  status
 50               51           enabled
100               99           enabled
122               865          disable
NNN               offern       disable

Database values

      while($row = mysql_fetch_array($sql))  
{
$offername[] = $row['name'];
$offercurrent[] = $row['current'];
$offerstatus[] = $row['status'];
$offerpoints[] = $row['points'];

}

Question:

The code should run only if offerstatus= enabled . one this condition is met . it should check all the occurences of offercurrent and offerpoints.
I have tried to replace the previous code with this one

    $check=false;
    for ($i=0;$i<count($offerstatus);$i++) { 
           //echo "$offerstatus[$i]";
            if ($offerstatus[$i]=="enabled")  { 
           if($offercurrent[$i]>=$offerpoints[$i]) {$check=true;}
            echo "$offername[$i]";}
                                          } 

    echo "$check";

if ($check=true) { //run code } 
else {//error messages}

With this code check alyways return 1 (true) it only checks the first values and ignores the rest of the values.

Community
  • 1
  • 1
Asif
  • 77
  • 1
  • 8
  • you're assigning true to $check inside the if – gontrollez Mar 04 '16 at 11:12
  • if($offercurrent[$i]>=$offerpoints[$i]) . if this is true then check will be true. – Asif Mar 04 '16 at 11:15
  • `if ($check=true) ` needs to be `if ($check === true) ` or `if ($check == true) ` using a single `=` assigns a value to the variable rather than checking it. – Epodax Mar 04 '16 at 11:54
  • Possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Epodax Mar 04 '16 at 11:56

1 Answers1

0

This problem was solved by adding else in the code .

$check=false;
for ($i=0;$i<=count($offerstatus);$i++) { 
       //echo "$offerstatus[$i]";
        if ($offerstatus[$i]=="enabled")  { 
       if($offercurrent[$i]>=$offerpoints[$i]) {$check=true;}
                       else{  $check=false;   
        break; //if $condition is less than $offer it will get out of loop.
                                   }
        }
                                      } 

if ($check=true) { //run code } 
else {//error messages}
Asif
  • 77
  • 1
  • 8