我想显示列状态,例如,如果剩余天数为30天,则很危险

时间:2019-02-26 08:03:23

标签: php html mysql html5

我希望状态与day_left有关系,例如如果day_left有30天,则status应该是danger。这是作业

<?php
$con2= mysqli_connect("localhost","root","root","database") or die("Error: " . mysqli_error($con2)); 
 
$query2 = "SELECT *,datediff (mainexpire,now())   as day_left  from contact";
$result2 = mysqli_query($con2, $query2);






echo "<table border='1'  align='center' width='1000'>";




echo "<tr bgcolor='#FFFACD'><td><p><center><b>no</center></td></p></b><td><p><center><b>maintenance items 
</center></td></p></b><td><p><center><b>owner
</td></p></center></b>
<td><p><center><b>detail
</center></td></p></b><td><p><center><b>expire_date
</center></td></p></b><td><p><center><b>Days Left
</center></td></p></b><td><p><center><b>Status
</center></td></p></b>     
</tr>";

while($row2 = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td><center><p>" .$row2["no"] .  "</center></td></p> ";
echo "<td><center><p>" .$row2["mainitem"] .  "</center></td></p> ";
echo "<td><center><p>" .$row2["mainowner"] .  "</center></td></p> ";
echo "<td><center><p>" .$row2["maindetail"] .  "</center></td></p> ";
echo "<td><center><p>" .$row2["mainexpire"] .  "</center></td></p> ";
echo "<td><center><p>" .$row2["day_left"] .  "</center></td></p> ";
}
?>
</body>

2 个答案:

答案 0 :(得分:0)

function day_left($day){ //$day will be the target date
 $target_date= strtotime($day);  //this convert the target day to second
 $today = strtotime("today"); //this convert the current day to second
   $day_sec = $target_date- $today; //so target day minus current day second
   $total_day_left = $day_sec / 86400; //divide it to 86400 thats equivalent to one day
    return $total_day_left; //the total day left
}

答案 1 :(得分:0)

好吧,你做得很好(当然,如果你自己做的话)。只需为if else添加一个status条件。在while循环的最后一行之后,出现了类似的情况。

if( $row2["day_left"] > 30 ) { 
    echo "<td><center><p>All OK</center></td></p> "; 
} else { 
    echo "<td><center><p>Danger</center></td></p> "; 
}