Page 1 of 1

coloring rows depending on value

Posted: Mon Nov 23, 2009 5:25 am
by thadson
I'm running this code to display a table from a mysql database:

Code: Select all

 
<?php
session_start();
require_once('inc/constant.php');
require_once('inc/function.php');  //basic functions
db_on();
 
$table = 'mytable';
 
// sending query
$sql = "SELECT `name` AS `Name`, `yesterday` AS `Yesterday`, `today` AS `Today`, `difference` AS `Difference`, `quota` AS `Quota`, `date` AS `Date` FROM {$table} WHERE `active` = 1 ";
$res=mysql_query($sql,_sql);
if (!$res) {
    die("Query to show fields from table failed");
}
 
$fields_num = mysql_num_fields($res);
 
echo "<table border='1' align='center'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($res);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
$i=0; 
  while($row = mysql_fetch_row($res)){  
echo "<tr>"; 
      if (($i%2)==0){  
      foreach($row as $cell)  
echo "<td><font color='FF0000'>$cell</font></td>"; 
      } 
      else{  
        foreach($row as $cell)  
echo "<td><font color='0000FF'>$cell</font></td>"; 
      } 
    $i++;  
echo "</tr>\n"; 
  }
mysql_free_result($res);
?>
 
Right now, this have the rows in the table alternate from red to blue.

However I actually would like to have the rows red only if the value in 'difference' is negative (ie -1000) and have it blue only if the value in 'difference' is positive (ie 1000) and if it is 0, it can stay black.
Is there a way to do this?

Also, is there a way to calculate a value to show up in the table like (in layman's terms:)
'difference' = 'today' - 'yesterday' - 'difference' from the above query,
instead of the original value in the database, without changing the value in the database?

Re: coloring rows depending on value

Posted: Mon Nov 23, 2009 6:04 am
by Apollo
Sure, instead of the if (($i%2)==0) part, do something like

Code: Select all

// determine color for this row (depends on difference)
$rowColor = ($row['Difference']>0) ? '#0000FF' : (($row['Difference']<0) ? '#FF0000' : '#000000');
 
// now print entire row using the fancy color we just picked
foreach($row as $cell)
{
    echo "<td><font color='$rowColor'>$cell</font></td>"; 
}

Re: coloring rows depending on value

Posted: Mon Nov 23, 2009 1:14 pm
by thadson
Apollo wrote:Sure, instead of the if (($i%2)==0) part, do something like

Code: Select all

// determine color for this row (depends on difference)
$rowColor = ($row['Difference']>0) ? '#0000FF' : (($row['Difference']<0) ? '#FF0000' : '#000000');
 
// now print entire row using the fancy color we just picked
foreach($row as $cell)
{
    echo "<td><font color='$rowColor'>$cell</font></td>"; 
}
Thanks. I tried your code with both 'Difference' and 'difference', and it always displays everything in black.

Re: coloring rows depending on value

Posted: Mon Nov 23, 2009 2:46 pm
by Apollo
Oh wait, you're using mysql_fetch_row to get the row (why?). That gets you a numerically indexed array, so you'd need $row[3] rather than $row['Difference'].

But perhaps it's better (more readable & less error prone if you insert other fields later on) to use mysql_fetch_assoc instead.

Re: coloring rows depending on value

Posted: Mon Nov 23, 2009 3:11 pm
by thadson
Thank you,

I can see now the error of my ways :D

That worked !!!

Thanks again