按日期对结果进行排序和分组 [英] Sort and group results by date
本文介绍了按日期对结果进行排序和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张包含博客文章的表格.我想选择最新的帖子并使用 PHP 在它们之间回显一个分隔符,类似于下面的示例.我正在考虑将所有结果存储在 PHP 数组中然后回显,是否有更简单的方法通过 MySQL?
I have a table with blog posts. I want to select the most recent posts and using PHP echo a divider between them similar to the example below. I was thinking about storing all results in PHP arrays and then echoing, is there an easier way via MySQL?
Thursday, Sept 8th
-----------------------
Post 1
Post 2
Friday, Sept 9th
-----------------------
post 3
post 4
推荐答案
这是一个想法:
<?php
$sql = "SELECT * FROM posts ORDER by post_date DESC";
$res = mysql_query($sql);
$previous_date = "";
while($post = mysql_fetch_assoc($result)){
//here goes the date
if($post['post_date'] != $previous_date){
echo $post['post_date'];
echo "<br>--------------------<br>";
$previous_date = $post['post_date'];
}
//here goes the post
echo "Post:{$post['title']}<br>";
}
?>
这篇关于按日期对结果进行排序和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文