【问题标题】:PHP List - Conditional FormattingPHP 列表 - 条件格式
【发布时间】:2015-05-20 05:44:43
【问题描述】:

我有一个包含续订日期列表的 PHP。日期字段为“日期时间”。

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

例如

约翰·多伊先生 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>"; 
?> 

【问题讨论】:

  • 请添加您目前所做的,帮助您会容易得多。
  • 感谢我编辑了问题
  • 您需要计算现在和您的日期之间的差异,您可以从差异中查看这里stackoverflow.com/questions/676824/…

标签: php list formatting conditional


【解决方案1】:
        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>"; 
        } 

【讨论】:

    【解决方案2】:

    假设 $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'>";
            (...)
    ?>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多