01hanstu Posted January 31, 2010 Share Posted January 31, 2010 Hi, Is there anyway the if the result from the database value is greater than 3 then set cell color of table. i.e. if result = 3 the bg color=red if result = >3 the bg color=blue else color = white. Any Ideas Quote Link to comment https://forums.phpfreaks.com/topic/190445-phpresults/ Share on other sites More sharing options...
wildteen88 Posted January 31, 2010 Share Posted January 31, 2010 To get the number of results a query returned use mysql_num_rows, example $total_rows = mysql_num_rows(QUERY RESULT VARIABLE HERE); To set the background color based on what $total_rows is set to you'd use an if/elseif/else statement // set $bgcolor based $total_rows // if $total_rows is equal to 3, bcolor is set to red if($total_rows == 3) $bgcolor = 'red'; // if $total_rows is greater than 3, bcolor is set to blue elseif($total_rows > 3) $bgcolor = 'blue'; // $total_rows is not equal to 3 or is not greater than 3, bgcolor is set to white else $bgcolor = 'white'; To change the background of the table you can either use the bgcolor html attribute, example <table bgcolor="<?php echo $bgcolor; ?>"> <tr><td>some cells</td></tr> </table> Or using (inline ) CSS, example <table style="background-color: <?php echo $bgcolor; ?>"> <tr><td>some cells</td></tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/190445-phpresults/#findComment-1004606 Share on other sites More sharing options...
01hanstu Posted January 31, 2010 Author Share Posted January 31, 2010 Hi, Thanks wildteen88. Unfortunately what I need is for it to read the data from the table, print it and if the data is more that 3 characters then bg color of td = red, if it is 3 then print blue. Its a booking system and when staff book, it uses there name (Which is more that 3 chars), but when we fill in the permanent bookings then we use the staff code (Three letters). Thanks Stu Quote Link to comment https://forums.phpfreaks.com/topic/190445-phpresults/#findComment-1004703 Share on other sites More sharing options...
jl5501 Posted January 31, 2010 Share Posted January 31, 2010 That is completely different from your original question where you asked about the number of rows returned from a query. You can however use a similar technique but based on the strlen() of the variable Quote Link to comment https://forums.phpfreaks.com/topic/190445-phpresults/#findComment-1004704 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.