开发者

PHP - Displaying Blog Menu by Date

I have created a function to display the dates and the count of posts within those dates to be used as my blog menu, however it only partially works, any help with this would be appreciated.

Function being called:

function displayBlogMenu()
{
    blogDBSelect();
    $sql = "SELECT datePosted FROM blog_entries";
    $results = mysql_query($sql);           
    while($post = mysql_fetch_array($results))
    {
        $startDate = date('Y-m-01', strtotime($post['datePosted']));
        $endDate = date('Y-m-01',  strtotime("+1 month", strtotime($post['datePosted'])));  
        $sql2 = "SELECT * FROM blog_entries WHERE datePosted >= '$startDate' AND datePosted < '$endDate'";
        $results2 = mysql_query($sql2); 
        $count = mysql_num_rows($results2);
    }                                   
    echo '<a href="">'.date('F, Y', strtotime($startDate)).' ('.$count.' Posts)</a><br>';
}

I want the function to return the date as links such as:

June, 2011 (10 Posts开发者_运维百科)
July, 2011 (2 Posts)


It looks like you can do that by one single query

SELECT Date_format(dateposted, '%M, %Y'), 
       COUNT(*) AS cc 
FROM   blog_entries 
GROUP  BY YEAR(dateposted), 
          MONTH(dateposted) 
ORDER  BY COUNT(*) DESC, 
          dateposted DESC 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜