Jump to content

[SOLVED] Change background color if days > 7


nashsaint

Recommended Posts

Hi,

 

I managed to display SQL records with an interval background color inside a table.  Now, I want to put a condition to check how long it has been posted and change the background color to, say, yellow if more than 1 week old, and red if more than 2 weeks..

 

As a point of reference I created an sql field named 'date_requested', maybe to compute the date against now(), but I dont know how to do it. 

 

Please help.  Below is my code.

 

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#B2C8E8' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['disk_id'] . '</td>
                <td align="left"><b>' . $row['eng_name'] . '</b></td>
	<td align="left"><b>' . $row['job_no'] . '</b></td>
	<td align="left"> ' . $row['model_no'] . '</td>
</tr>
';
}

try

<?php
// Fetch and print all the records.

$sql = "SELECT disk_id, eng_name, job_no, model_no, 
        DATEDIFF(CURDATE(), date_requested) as days
        FROM mytablename ";
$result = mysql_query($sql);
$count = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    switch (true) {
        case $row['days'] > 14: $bg = '#ff0000'; break;
        case $row['days'] > 7:  $bg = '#ffff00'; break;
        default: $bg = $count++%2 ? '#B2C8E8' : '#eeeeee';
    }

echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['disk_id'] . '</td>
                <td align="left"><b>' . $row['eng_name'] . '</b></td>
	<td align="left"><b>' . $row['job_no'] . '</b></td>
	<td align="left"> ' . $row['model_no'] . '</td>
</tr>
';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.