单个日期标题下的PHP回显结果

时间:2016-09-20 20:11:31

标签: php

我有一个我正在查询的大型数据库。有多个结果共享相同的“日期”。所以,我今天可能有5个结果,昨天有8个结果,依此类推。

我回应结果,以便每篇文章现在标题下方都有“日期”。但是,我正在搜索如何显示DATE HEADER,然后在每个分组日期下面的文章标题。

SO:

2016年9月19日

  • 第1条标题
  • 第2条标题
  • 第3条标题
  • 第4条标题

2016年9月17日

  • 第1_1条标题
  • 第2_1条标题
  • 第3_1条标题

......等等。

这是我的MYSQL的快照。非常感谢高级NINJAS !!

enter image description here

代码如下:

<?php do { ?>

<b><?php echo date('M. j, Y', ($row_Recordset2['date'])); ?></b><br />
<b><a href="/full.php?id=<?php echo $row_Recordset2['id']; ?>"><?php echo stripslashes(htmlspecialchars($row_Recordset2['title'])); ?></a></b><br />
<?php echo (stripslashes(str_replace("{nl}", "<br />", $row_Recordset2['short']))); ?>


<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>

1 个答案:

答案 0 :(得分:1)

1)不要使用do..while,只使用while 2)

<?php $current_date = '';
    while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)){
      if($current_date !== $row_Recordset2['date']){
        echo '<b>' . date('M. j, Y', ($row_Recordset2['date'])) . '</b>';
        $current_date = $row_Recordset['date'];
    }
      echo "<b><a href=\"/full.php?id=$row_Recordset2[id]\">".     stripslashes(htmlspecialchars($row_Recordset2['title'])) . "</a></b><br />" .
      (stripslashes(str_replace("{nl}", "<br />", $row_Recordset2['short'])));
    }