3

I have a while loop where I want to compare the current variable ($high) to the previous one. If the value is higher I want to show a green background and if it is lower I want to show a red background.

At the moment, if the value is higher, then it shows a red background, also it only seems to compare the latest result and not all of the values in the loop.

Link to test page

$previous = 0;
while ($row =mysql_fetch_assoc($result)){
    $high = $row['High'];
    if ($high > $previous){
        ?> <span style="background:green"><?php echo $row['High']; ?></span><?PHP
    }else{
        ?> <span style="background:red"><?php echo $row['High']; ?></span><? PHP
    }
    $previous = row;
}
2
  • 4
    1.mysql_* is deprecated and removed library now, so upgrade yourself to PHP7 along with mysqli_* or PDO. 2. $previous = row; needs to be $previous = $high; Commented Sep 17, 2018 at 10:48
  • 1
    you are assigning the entire $row dataset to $previous, which will be an array, and that will be used to compare instead of the actual value of $row['High']; Commented Sep 17, 2018 at 10:54

2 Answers 2

1

mysql_* is deprecated and removed library now, so upgrade yourself to PHP7 along with mysqli_* or PDO libraries.

$previous = row; needs to be $previous = $high;

Output:-https://eval.in/1058293

Sign up to request clarification or add additional context in comments.

Comments

1

At present the code stores the previous $row, not he specific $row['High']. Please change

$previous = $row;

to

$previous = $row['High'];

1 Comment

Or even $previous = $high;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.