php到期日交通灯系统

时间:2014-05-02 08:41:10

标签: php mysql css

我正在研究红绿灯系统。它有点乱,而且它没有按照应有的方式工作。

我的目标是让系统像这样工作;

在数据库中有供应商保险到期的日期。如果到期日期是30天或更短时间,那么我希望这些日期可以通过并回收,但是其他在30天后过期的日期不应该显示。

目前它还给出了到期日之前的天数,但这不能正常工作,所以说今天是5月2日,明天是第3天;保险单据将于3日到期,因此应该说明天保险到期。这目前无法正常工作,我得到了类似于' 2天的事情。直到到期而不是明天到期'

同样明智的是,如果保险今天到期,它应该说今天到期,但它没有让日子正确。

另外,我设计了不同颜色的div标签,根据保险文档到期的日期显示为一种交通灯信号。如果文档将在30到20天内到期,我希望我的绿色div显示,否则如果文档将在19-7天到期,我希望它显示为琥珀色,如果文档即将到期7- 0天或结束,因为它应该是红色的。

这是我的代码,请有人告诉我如何改进它以完成我需要它做的事,谢谢

CODE:

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

         echo "<table class=\"table\" style=\"width:995px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
         font-size:11px;\" >

    <tr>
        <td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Owner:</td><td style=\"width:200px;\">Note:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";


         while($row = mysql_fetch_array( $data )) { 
           $days = $row['expire_date'] -1;


           echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>"; 
           echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>"; 
           echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
           echo "<td style=\"width:100px;\"><p>".$row['owner'] . "</p></td>";

           if ($days > 0) {
                echo "<td style=\"width:200px;\"><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>"; 
            } else {
              $when = $days*-1;           

              echo "<td style=\"width:200px;\"><p>Insurance expires";

              if ($when > 1){
                  echo " in <font color=\"red\">{$when} days</font></p></td>";

              } elseif ($when < 1) {
                echo "<font color=\"red\"> tomorrow!</font></td>";
              }
            elseif ($when == 0) 
            {
                echo " today!</font></p></td>";
            }

            echo "<td>";
              echo date('d/m/Y',strtotime($row['insurance_date']));   

      echo"</td>";


            }
            if ($when >= 20){
                echo "<td style=\"width:150px;\"><div class=\"green_light\"></div></td>";
              }

              if ($when >= 7){
                echo "<td style=\"width:150px;\"><div class=\"amber_light\"></div></td>";
              }

 if ($when <= 7){
                echo "<td style=\"width:150px;\"><div class=\"red_light\"></div></td>";
              }


            echo "<tr>";
          }


          echo "</table>"; //Close the table in HTML


        ?>

1 个答案:

答案 0 :(得分:0)

更改查询以准确反映expiry_date之前的天数,然后它应简化检查。下面的查询截断到当天,并将准确的days_before_expires作为正数。

正数是前几天。所以+1意味着明天结束。

零表示今天结束

负数表示已过期。

SELECT *, TIMESTAMPDIFF(DAY,  CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires FROM supplier_stats

转换为'mysqli'。

所有代码都经过测试。它实现了“交通灯”状态。我已经更改了格式化代码,尝试通过使用函数来计算“紧急度指标”来使其更易于维护。所有格式化现在都是使用css完成的。

<!DOCTYPE HTML">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>23424062/php-expiry-date-traffic-light-system</title>
    <style type="text/css">
        table {
            width:995px;
            font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
            font-size:11px;
        }
        th {text-align: left; }
        .width_small {
            width: 100px;
        }

        .width_medium {
            width: 150px;
        }

        .width_large {
            width: 200px;
        }
        .urgency_1 {
            display: inline-block;
            width: 6em;
            background-color: green;
            color: yellow;
            padding-left: 1em;
        }
        .urgency_2 {
            display: inline-block;
            width: 6em;
            background-color: orange;
            padding-left: 1em;
        }
        .urgency_3 {
            display: inline-block;
            width: 6em;
            background-color:  red;
            color: yellow;
            padding-left: 1em;
        }

        .green_light {
            width: 3em;
            background-color: green;
        }
        .amber_light {
            width: 3em;
            background-color: orange;
        }
        .red_light {
            width: 3em;
            background-color:  red;
        }
    </style>
  </head>

  <body>
<?php
$mysqli = new mysqli('localhost', 'test', 'test', 'testmysql');


$data = $mysqli->query("SELECT *, "
                       ." TIMESTAMPDIFF(DAY,  CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires, "
                       ." DATE(insurance_date) as insurance_day "
                       ." FROM supplier_stats  order by DATE(insurance_date)")
  or die(mysql_error());
?>
<table>
<tr><th class="width_small">ID:</th><th>Company Name:</th><th>Company Reg No:</th><th>Owner:</th><th class="width_large">Note:</th><th class="width_small">Date:</th><th>Status:</th></tr>

<?php while($row = $data->fetch_array()): ?>
    <?php $daysLeft = $row['days_before_expires']; ?>
    <?php $urgency = getUrgency($daysLeft); ?>
    <?php $cssTrafficLight = selectUrgencyLit($row['days_before_expires'],
                                       array('', 'green_light', 'amber_light', 'red_light')); ?>
    <?php $cssUrgency = selectUrgencyLit($row['days_before_expires'],
                                       array('urgency_0', 'urgency_1', 'urgency_2', 'urgency_3')); ?>
    <?php $daysLeftLit = getDaysLeftLiteral($daysLeft); ?>

    <tr><td class="width_small"><p><?= $row['id'] ?></p></td>
    <td class="width_medium"><p><?= $row['company_name'] ?></p></td>
    <td class="width_medium"><p><?= $row['company_reg_number'] ?></p></td>
    <td class="width_small"><p><?= $row['owner'] ?></p></td>
    <td class="width_large"><?= $daysLeftLit; ?></td>

    <td class="width_small"><?= date('d/m/Y',strtotime($row['insurance_day'])) ?></td>
    <td class="width_medium"><div class="<?= $cssTrafficLight?>">&nbsp;</div></td>
    <tr>
  <?php endwhile; ?>
  </table> <!-- Close the table in HTML -->
  </body>
</html>

    <?php
/*
 * To tidy the code up and hopefully make it easier to maintain we need
 * a couple of functions...
 *
 * How about assigning an 'urgency level' to each record depending on the
 * days left before the expiry date. The level is as follows:
 *
 * 0 => not urgent  (more than 20 days)
 * 1 => need to pay attention (14 - 20)
 * 2 => soon (7 - 13)
 * 3 => now  (0 - 6)
 */
function getUrgency($daysLeft)
{
    if ($daysLeft >= 21 || $daysLeft < 0) { return 0; }
    if ($daysLeft >= 14  && $daysLeft <= 20) { return 1; }
    if ($daysLeft >=  7  && $daysLeft <= 13) { return 2; }
    return 3;
}

// return a literal depending on the urgency
function selectUrgencyLit($daysLeft, array $litArray)
{
    $urgency = getUrgency($daysLeft);
    if (!empty($litArray[$urgency])) {
        return $litArray[$urgency];
    }
    return '';
}

// return the days left literal
function getDaysLeftLiteral($daysLeft)
{
    $urgency = getUrgency($daysLeft);
    if ($urgency <= 0) {
       return '&nbsp;';
    }

     $text = "Insurance Expires: <div class=\"urgency_{$urgency}\">";

    // set the day literal to be shown
    if  ($daysLeft == 0) { $dayLit = 'today'; }
    elseif ($daysLeft == 1)  { $dayLit = 'tomorrow'; }
    elseif ($daysLeft >= 0 && $daysLeft <= 20)  { $dayLit = "in {$daysLeft} days"; }
    else { $dayLit = '&nbsp;'; }

    return $text . $dayLit .'</div>';
}