coloring rows depending on value

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
thadson
Forum Newbie
Posts: 19
Joined: Fri Jul 15, 2005 12:29 pm

coloring rows depending on value

Post 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?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: coloring rows depending on value

Post 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>"; 
}
thadson
Forum Newbie
Posts: 19
Joined: Fri Jul 15, 2005 12:29 pm

Re: coloring rows depending on value

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: coloring rows depending on value

Post 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.
thadson
Forum Newbie
Posts: 19
Joined: Fri Jul 15, 2005 12:29 pm

Re: coloring rows depending on value

Post by thadson »

Thank you,

I can see now the error of my ways :D

That worked !!!

Thanks again
Post Reply