blackholesun Posted May 16, 2013 Share Posted May 16, 2013 (edited) Hi all, If anyone can offer any advice on the following, that would be great... I've been running round in circles on the internet trying to find this answer but am having no success... All i want to be able to do is to format a MySQL query result so that a negative number is red and a positive number is blue. So far, the query runs fine, the results are displayed largely in the format i want, i am just trying to do this one last refinement. I'm thinking something along the lines of: if($row['number'] < 0){ //display red }else{ //display blue } ...but what i have attempted with that conditional will not write to the table output. I'm probably barking up the wrong tree with the if..else conditional but am still new to php so am having trouble coming up with the right solution. So, is there a way of doing this just with php? Or am I going to have to use something else? Any help/info greatly appreciated Edited May 16, 2013 by blackholesun Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/ Share on other sites More sharing options...
cpd Posted May 16, 2013 Share Posted May 16, 2013 if(($id % 2) == 0) echo '<td class="red">'; else echo '<td class="blue">'; Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/#findComment-1430553 Share on other sites More sharing options...
blackholesun Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) if(($id % 2) == 0) echo '<td class="red">'; else echo '<td class="blue">'; Is the '$id' in your example the same as the $row['number'] in mine? Edited May 16, 2013 by blackholesun Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/#findComment-1430556 Share on other sites More sharing options...
cpd Posted May 16, 2013 Share Posted May 16, 2013 You've used $row['number']; so assuming that's what you meant and this is a uniquely identifying integer for the record then yes. Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/#findComment-1430557 Share on other sites More sharing options...
akphidelt2007 Posted May 16, 2013 Share Posted May 16, 2013 I usually do this like this if($row['result'] > 0) $color = 'blue'; elseif($row['result'] < 0) $color = 'red'; else $color = 'black; echo "<span style='color:$color;'>{$row['result']}</span>"; Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/#findComment-1430558 Share on other sites More sharing options...
akphidelt2007 Posted May 16, 2013 Share Posted May 16, 2013 Or if you want 0 to blue you can do the shortcut... $color = $row['result'] < 0 ? 'red' : 'blue'; echo "<span style='color:$color;'>{$row['result']}</span>"; Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/#findComment-1430559 Share on other sites More sharing options...
blackholesun Posted May 16, 2013 Author Share Posted May 16, 2013 Brilliant! Thanks both for the replies, they were most helpful Problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/278081-formatting-numbers-in-different-colors/#findComment-1430569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.