Change font color in table based on value

I have a statistical table hat I populate using mysql database. I have a field with a percentage value. When the result is positive I would like the percentage number to display green, when negative I would like it to be red, and black when 0. How can i accomplish this using php. I have tried a number of different options as found online with no success. I am not sure if the issue is with my code or mysql setup? The data populates correctly but no color change? Here is what I have currently:

<?php $sql = "SELECT * FROM datasheet WHERE id='1'"; $result = mysql_query($sql); $row = mysql_fetch_object($result); if ($var < 0) $style="#00FF00"; else if ($var < 0) $style="#FF0000"; else $style = 0"#000000"; ?>
<? echo $row->change; ?>

It should be similar to this… In your code you weren’t setting $var…

[php]<?php
$sql = “SELECT * FROM datasheet WHERE id=‘1’”;
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
if ($row->change > 0) $style="#00FF00";
else if ($row->change < 0) $style="#FF0000";
else $style ="#000000";
?>

<? echo $row->change; ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service