php show red, amber or green colour div depending

2019-09-24 10:38发布

This question already has an answer here:

i have a php script that tells the user when a document is about to expire. I now want to amend this script and add traffic lighting to it, by this i mean if a document is due to expire in 7 days or less then show a red colour div, otherwise if a document is due to expire in 30 days or less then show a amber div, or if a document is not due to expire prior to 30 days then show a green colour div.

can someone please show me how i can amend my script to do what i need it to do, thanks.

php:

        <?php 
 include 'config.php';
 $data = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date 
  FROM supplier_stats") 
 or die(mysql_error()); ?>



            <?php 
include 'config.php';
$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date 
                         FROM supplier_stats") or die(mysql_error()); 
while($row = mysql_fetch_array($result)) 
{
    $days = $row['expire_date'];
    if ($days > 0)
    {
        echo "<p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)</font></p>"; 
    }
    else
    {
        $when = $days*-1;            
        echo "<p><font color=\"red\">Insurance expires";
        if ($when > 1)
        {
            echo " {$when} days&nbsp;|&nbsp;<font color=\"red\">"; 
            $data = mysql_query("SELECT * FROM supplier_stats") 
            or die(mysql_error()); 

            while($info = mysql_fetch_array( $data )) 
            { 
            echo date('d/m/Y',strtotime($info['insurance_date'])); echo"</font></p>";  } 
            }
        elseif ($when == 1)
        {
            echo " yesterday!</p>";
        }
        else
        {
            echo " today!</font></p>";
        }

    }

} 
?>

标签: php mysql
1条回答
时光不老,我们不散
2楼-- · 2019-09-24 11:24

If I was you I would make a function that maps days left to color:

function traffic_light($days){
  if($days <= 7) return 'red';
  else if($days <= 30) return 'yellow';
  else return 'green';
}

And then alter the code that displays lines to use it to select the color:

echo "<p>Insurance expires in <font color=\"".traffic_light($days)."\">{$row['expire_date']} day(s)</font></p>";
查看更多
登录 后发表回答