nashsaint Posted May 5, 2008 Share Posted May 5, 2008 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> '; } Link to comment https://forums.phpfreaks.com/topic/104179-solved-change-background-color-if-days-7/ Share on other sites More sharing options...
Barand Posted May 5, 2008 Share Posted May 5, 2008 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> '; } ?> Link to comment https://forums.phpfreaks.com/topic/104179-solved-change-background-color-if-days-7/#findComment-533346 Share on other sites More sharing options...
nashsaint Posted May 5, 2008 Author Share Posted May 5, 2008 It works like a charm. Thanks a lot Barand.. Link to comment https://forums.phpfreaks.com/topic/104179-solved-change-background-color-if-days-7/#findComment-533469 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.