繁体   English   中英

如何在同一日期下显示多个帖子而不在循环中重复相同的日期(php)

[英]how to display multiple posts under the same date without repeating the same date in the loop (php)

我想在帖子数组上方显示日期,而对具有相同日期的帖子不重复日期。

2018年12月3日
岗位
岗位
岗位

代替 :

2018年12月3日
岗位

2018年12月3日
岗位

二〇一八年十二月十三日
岗位

<?php global $connection;
$query = "SELECT posts.post_timestamp, posts.post_id, posts.post_title FROM locations, posts WHERE posts.post_location=locations.id && id=$id "; //Display posts for selected location
$select_posts = mysqli_query($connection,$query);

while($row = mysqli_fetch_assoc($select_posts)) {
    $post_id = $row['post_id'];
    $post_title = $row['post_title'];
    $post_timestamp = $row['post_timestamp'];

?>

<h4><?php echo date( "l, d F Y", strtotime($post_timestamp));?></h4> //Display date for all posts with the same date
<h1><a href="post.php?post_id=<?php echo $post_id ;?>"><?php echo $post_title ;?></a></h1> //Print post title

<?php } ?>

一种方法是跟踪时间戳,并仅在更改时才打印

 <?php global $connection; $query = "SELECT posts.post_timestamp, posts.post_id, posts.post_title FROM locations, posts WHERE posts.post_location=locations.id && id=$id "; //Display posts for selected location $select_posts = mysqli_query($connection,$query); $keepTimestamp = ""; while($row = mysqli_fetch_assoc($select_posts)) { $post_id = $row['post_id']; $post_title = $row['post_title']; $post_timestamp = $row['post_timestamp']; // if ($keepTimestamp != $post_timestamp){ $keepTimestamp = $post_timestamp; ?> <h4> <?php echo date( "l, d FY", strtotime($post_timestamp));?> </h4> //Display date for all posts with the same date <?php } ?> <h1> <a href="post.php?post_id=<?php echo $post_id ;?>"> <?php echo $post_title ;?> </a> </h1> //Print post title <?php } ?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM