我想显示列状态,如果剩下的那天有30天的状态就危险了

问题描述 投票:0回答:2

我希望状态与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>
php html mysql html5
2个回答
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> "; 
}

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
}
© www.soinside.com 2019 - 2024. All rights reserved.