Jump to content

Cell background colour by date


davidcriniti

Recommended Posts

Hi everyone,

 

In the script below, the background colour (I'm Australian, not a bad speller  8) ) is determined by the sex field.

 

Any tips on how I can change the array (or replace it with an if / else statement) so it's determined by the 'note_end_date' field and set out so that if the value in the note_end_date is sometime in the future, then the cell background is #FF99FF, and if that is not the case, then the cell background is #CCCCCC.

 

Any advice is much appreciated. Here's the relevant code:

 

 

$bgColors = array('F'=>'#FF99FF', 'M'=>'#CCCCCC'); while ($row = mysql_fetch_array($sql))

{   
// Print out the contents of each row into a table   
echo "<tr bgcolor=\"{$bgColors[$row['sex']]}\">\n";    


	echo "</td><td>";
echo $row_counter++; 
echo "</td>";
echo "<td>{$row['firstname']}</td>\n";    
echo "<td>{$row['lastname']}</td>\n";   
echo "<td>{$row['sex']}</td>\n";    
echo "<td>{$row['notes']}</td>\n";   
echo "<td>{$row['note_end_date']}</td>\n";   
echo "</tr>\n";
}
echo "</table>";

Link to comment
Share on other sites

off the top of my head, maybe something like this: (untested)

<?php
while ($row = mysql_fetch_array($sql)){   
$bgColors = strtotime($row['note_end_date']) > time() ? '#FF99FF' : '#CCCCCC');
// Print out the contents of each row into a table   
echo "<tr bgcolor=\"{$bgColors[$row['sex']]}\">\n";    


	echo "</td><td>";
echo $row_counter++; 
echo "</td>";
echo "<td>{$row['firstname']}</td>\n";    
echo "<td>{$row['lastname']}</td>\n";   
echo "<td>{$row['sex']}</td>\n";    
echo "<td>{$row['notes']}</td>\n";   
echo "<td>{$row['note_end_date']}</td>\n";   
echo "</tr>\n";
}
echo "</table>";

?>

Link to comment
Share on other sites

WebStyles missed changing this line:

echo "<tr bgcolor=\"{$bgColors[$row['sex']]}\">\n";
//should be
echo "<tr bgcolor=\"{$bgColors}\">\n";

 

If the column 'note_end_date' is a date, datetime, or timestamp column.

Link to comment
Share on other sites

Thanks webstyles and jcbones.

 

I've now got a solution thanks to both of you. I'll post the working code below for the benefit of anyone wanting to do this in future:

 

<?php
while ($row = mysql_fetch_array($sql)){   
$bgColors = strtotime($row['note_end_date']) > time() ? '#99FF99' : '#FF3333';
// Print out the contents of each row into a table   
echo "<tr bgcolor=\"{$bgColors}\">\n";
	echo "</td><td>";
echo $row_counter++; 
echo "</td>";
echo "<td>{$row['firstname']}</td>\n";    
echo "<td>{$row['lastname']}</td>\n";   
echo "<td>{$row['sex']}</td>\n";    
echo "<td>{$row['notes']}</td>\n";   
echo "<td>{$row['note_end_date']}</td>\n";   
echo "</tr>\n";


}
echo "</table>";
?>

 

Thanks again,

 

Dave

Link to comment
Share on other sites

Having played with this over the past couple of days I got to thinking about the possibility of having a third colour (for example, #CC6600) for the background of the cells in a situation where note_end_date is nearing, but has not yet arrived.

 

Kind of like traffic signals I guess:  Green when the end date is a way off, orange when the end date is approaching (say less than 7 days away) and red for once the end date has arrived.

 

Currently the code just has the two options (green and red):

 

$bgColors = strtotime($row['note_end_date']) > time() ? '#99FF99' : '#FF3333';

 

Can anyone advise how I'd tweak it to include the third option?

 

Yours gratefully,

 

Dave

 

 

Link to comment
Share on other sites

calculate the remaining time with a simple substraction.

then use an IF statement to check if it's greater or smaller than 7 days (in seconds)

which would be 60*60*24*7 (seconds * minutes * hours * days)

 

so something like:

if(strtotime($row['note_end_date']) > time()){
  // date hasn't expired yet, so we check the remaining days
  $diff = time() - strtotime($row['note_end_date']);
  if($diff <= (60*60*24*7) ){
     $bgColors = '#CC6600';
  }else{
     $bgColors = '#99FF99';
  }
}else{
  // date has expired
  $bgColors = '#FF3333';
}

 

hope this helps

Link to comment
Share on other sites

Thanks Webstyles,

 

I managed to get it working after changing the code slightly to make it strtotime($row['note_end_date']) - time() instead of vice versa.

 

while ($row = mysql_fetch_array($sql)){ 

if(strtotime($row['note_end_date']) > time()){

  // date hasn't expired yet, so we check the remaining days

  $diff = strtotime($row['note_end_date']) - time();

  if($diff <=  (60*60*24*7) ){

    $bgColors = '#CC6600';

  }else{

    $bgColors = '#99FF99';

  }

}else{

  // date has expired

  $bgColors = '#FF3333';

}

 

 

Seems to be working like a charm now - ta muchly!

 

Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.