Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

php - Sort and group results by date

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an idea:

<?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>";
    }
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...