If you’re only returning one result from your query, I can’t see how that will work because you’re not retrieving the previous price, only the latest one. If you change the query to LIMIT 2, that would give you the current and latest record so you can do the comparison.
Have you changed the way you retrieve the data from your query? In your latest code you’ve removed the reference to the count element of the array, the [$i] section, so that may cause the error you are seeing.
Easy. Change your query to use LIMIT 2 instead of 1, this will return zero, one or two results. Then pseudo-code:
if resultcount = 0 do nothing
if resultcount = 1, set colour to black (nothing to compare to)
if resultcount = 2, set colour according to your rules
if resultcount > 0, display price
$colour = 'black';
$resultcount = count($pays);
if ($resultcount == 2) {
if ($pays[1]['price'] > $pays[0]['price']) $colour = 'red';
if ($pays[1]['price'] < $pays[0]['price']) $colour = 'green';
}
if ($resultcount > 0) { // can't display with no results
// display the price
}
if(isset($pays[0])) { // We have at least 1 value.
$start = start($pays);
$end = end($pays); //Note that this will be the same value as $start if only 1 record was returned.
echo "<span style='color:".(($start['price'] < $end['price']) ? "red" : (($start['price'] > $end['price']) ? "green" : "black"))."'>".$start['price']."</span>";
} else { echo "No Price Data"; }
(Note that your current code does not explicitly handle the case of $pays[0][‘price’] = $pays[1][‘price’].)
If only 1 value is returned into the $pays array, $start and $end will have the same value - and so the price will be equal, and thus black. (not >, not <). This also covers the case of there being identical pricing twice in a row.
For me, that was because I’d pre-set the colour variable to “black”, and only changed it if the latest was smaller or greater than the previous price. So if it was neither, it would remain at “black”. Useful to note end() function, I hadn’t seen that.