PHP列表 - 条件格式

时间:2015-03-17 16:56:36

标签: php list formatting conditional

我有一个包含续订日期列表的PHP。日期字段是“datetime”。

如何将样式类/ ID分配给日期在30天之内的行?

例如

John Doe先生31-03-2016 约翰先生考试31-05-2016

第一行将有一个“续订”类。这是迄今为止的

<?php   
$query = mysql_query("SELECT * FROM homepolicynumber order by id desc") 
or die(mysql_error()); 

echo "<table border='0' width='100%' cellpadding='10'>"; 
echo "<tr height='25px' valign='center' class='head'> 
    <th width='20px'><p></p></th> 
    <th width='140px'><p>Policy Number</p></th> 
    <th width='300px'><p>Customer Name</p></th> 
    <th width='115px'><p>Policy</p></th> 
    <th width='115px'><p>Date</p></th> 
    </tr>"; 

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

    echo "<tr height='25px' valign='center'>"; 
    echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img src="../../Images/Icons/table-delete.png"/></a></p></td>'; 
    echo '<td><p>' . $row['brand'] . '-' . $row['publication'] . '-R-' . $row['id'] . '</p></td>'; 
    echo '<td><p>' . $row['title'] . '&nbsp;' . $row['firstname'] . '&nbsp;' . $row['lastname'] . '</p></td>'; 
    echo '<td><p>Home</p></td>'; 
    echo '<td><p>' . $row['datetime'] . '</p></td>'; 
    echo "</tr>"; 
} 

// close table> 
echo "</table>"; 
echo "<br /><p><b>View All</b> | <a class='link' href='view-all.php?page=1'>View Paginated</a></p>"; 
?> 

2 个答案:

答案 0 :(得分:0)

        while($row = mysql_fetch_array( $query )) {
            $class = (strToTime($row['datetime'])>(time()-60*60*24*30))?' class="theClass"':''
            echo "<tr height='25px' valign='center'" . $class . ">";
            echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img src="../../Images/Icons/table-delete.png"/></a></p></td>';
            echo '<td><p>' . $row['brand'] . '-' . $row['publication'] . '-R-' . $row['id'] . '</p></td>';
            echo '<td><p>' . $row['title'] . '&nbsp;' . $row['firstname'] . '&nbsp;' . $row['lastname'] . '</p></td>';
            echo '<td><p>Home</p></td>';
            echo '<td><p>' . $row['datetime'] . '</p></td>';
            echo "</tr>"; 
        } 

答案 1 :(得分:0)

假设将来$ row ['datetime'],请尝试:

<?php
    while($row = mysql_fetch_array( $query )) {
        $days_diff = strtotime($row['datetime']) - strtotime(date("Y-m-d H:i:s"));
        if($days_diff >= 30)
            echo "<tr height='25px' class='the-class' valign='center'>";
        else
            echo "<tr height='25px' valign='center'>";
        (...)
?>

我没有测试代码但它应该可以工作。