我创建了一个功能来显示日期和这些日期内的帖子数,以用作我的博客菜单,但是它只能部分起作用,对此的任何帮助将不胜感激。

被调用的函数:

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>';
}


我希望函数将日期返回为链接,例如:

June, 2011 (10 Posts)
July, 2011 (2 Posts)

最佳答案

看起来您可以通过一个查询来做到这一点

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

10-07 13:57