php colour table data based on conditions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jv61
Forum Newbie
Posts: 2
Joined: Fri Mar 26, 2010 8:09 am

php colour table data based on conditions

Post by jv61 »

Hello all,
I am working on php code to colour the data in html table based on given conditions.
The data in second column should be in red colour if the data in fifth column is greater than 0.5 and turn green if it is less than 0.5. Similarly third column should apply the colour condition based on the data in sixth column. The code which I have given below applies the condition to all the data. But I need to colour the data based on the corresponding coulmn values.
I tried a lot, but couldn't figure out how to write a query statement to look for certain coulmn and based on that to colour the values in other column. Could anyone lead me to right direction? Any ideas would be much helpful. Thanks in advance.

Code: Select all

   while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
    {
      echo "<tr border='2' bordercolor= '#000033' bgcolor='#FFFFB5'>";
      foreach ($rows as $data){
 
        if($data >"0.5"){
        $bgcolor="red";
        }
        else{
        $bgcolor="green";
         } 
       echo "<td align='center' bgcolor='$bgcolor'>". $data . "</td>";
           }
}
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: php colour table data based on conditions

Post by ramblin54321 »

The error is on line 12. Your last echo statement has the variable $bgcolor as part of the string. Separate it with a dot like this

Code: Select all

echo ("<td align='center' bgcolor=" . $bgcolor . ">" . $data . "</td>";
or separate it with a separate echo statement like this:

Code: Select all

echo ("<td align='center' bgcolor="); echo($bgcolor); echo(">"); echo($data); echo("</td>");
jv61
Forum Newbie
Posts: 2
Joined: Fri Mar 26, 2010 8:09 am

Re: php colour table data based on conditions

Post by jv61 »

Thanks for the reply and correction. Any ideas how to colour the values in one column based on values in some other column?
Post Reply