Page 1 of 1

php colour table data based on conditions

Posted: Fri Mar 26, 2010 8:59 am
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>";
           }
}

Re: php colour table data based on conditions

Posted: Sat Mar 27, 2010 12:39 am
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>");

Re: php colour table data based on conditions

Posted: Sat Mar 27, 2010 10:43 am
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?