MySQL PHP:博客帖子存档下拉菜单

MySQL PHP:博客帖子存档下拉菜单,php,mysql,archive,Php,Mysql,Archive,我不熟悉MySQL和PHP,只是有几个问题 目前,我正在尝试为我的帖子创建一个归档菜单,在mysqldb命名的帖子中查找这些帖子 应该是这样的, 2010 (2) September (2) Bellavisa Mists of Netting July (1) Turkey is cool! 2009 (1) May (1) Cock of the Rock 但是我现在 2010 (2) September (2)

我不熟悉MySQL和PHP,只是有几个问题

目前,我正在尝试为我的帖子创建一个归档菜单,在mysqldb命名的帖子中查找这些帖子

应该是这样的,

2010 (2)
   September (2)
     Bellavisa
     Mists of Netting

   July (1)
     Turkey is cool!

2009 (1)
   May (1)
     Cock of the Rock
但是我现在

2010 (2)
   September (2)
     Bellavisa


   July (1)
     Turkey is cool!

2009 (1)
   May (1)
     Cock of the Rock
因此,我错过了9月份的第二个职位

任何帮助都将不胜感激!我的代码在下面

$sql = "SELECT Month(time) as Month, Year(time) as Year,
title, COUNT(*) AS total FROM posts GROUP BY Year, Month ORDER BY time DESC";
$stmt = $conn->query($sql);

$currentMonth = 0;

$currentYear = 0;

if ($stmt->num_rows > 0) {
while($row = $stmt->fetch_assoc()){
  $title = $row["title"];
     // if the year changes you have to display another year
    if($row['Year'] != $currentYear) {

        // reinitialize current month
        $currentMonth = 0;

        // display the current year
        #echo "<li class=\"cl-year\">{$row['Year']} ({$row['total']})</li>";
echo "          <ul>";
echo "          <li onClick = 'show(\"{$row['Year']}\")' > <img src='images/triangle_closed.gif' id=img_{$row['Year']}>{$row['Year']} ({$row['total']})</li>\n"; 
echo "          <li>\n"; 
echo "          <ul id = {$row['Year']} style='display:none;'>\n"; 
#echo "</ul>";



        // change the current year
        $currentYear = $row['Year'];
    }

    // if the month changes you have to display another month
    if($row['Month'] != $currentMonth) {

        // display the current month
        $monthName = date("F", mktime(0, 0, 0, $row['Month'], 10));
#echo "<ul>";
echo "              <li onClick = 'show(\"{$row['Year']}$monthName\")' > <img src='images/triangle_closed.gif' id=img_{$row['Year']}$monthName>$monthName ({$row['total']})</li>\n"; 
echo "              <li>\n"; 
echo "                <ul id = {$row['Year']}$monthName style='display:none;'>\n"; 
echo "                  <li class='cl-posts active'><a href='\base\item.php?id=$title'>".$title."</a></li>\n"; 
echo "                </ul>\n"; 
echo "              </li>\n"; 
        #echo "<li class=\"cl-month\">$monthName ({$row['total']})</li>";

        // change the current month
        $currentMonth = $row['Month'];
    }

    // display posts within the current month
    #echo "<li class='cl-posts active'><a href='\base\item.php?id=$title'>".$title."</a></li>";
}
}

 $conn->close();
 ?> 

谢谢大家!

查看您的查询,您是按年份分组的,因此不显示第二个标题是正常的,而且您还试图计算年份数。我建议混合使用一个子查询,这样您就可以同时得到两个结果,所以请更改

SELECT Month(time) as Month, Year(time) as Year, title, COUNT(*) AS total FROM posts GROUP BY Year, Month ORDER BY time DESC

此外,对于标识问题,每次一年或一个月发生变化时,您都要在关闭最后一个之前打开新的


验证$currentYear和$currentMonth,如果它们不为零,则必须先关闭,然后再打开新的。

您有什么问题?我想您只是错过了关闭HTML列表。使用编辑器缩进生成的HTML,您将看到。虽然它确实在9月份列出了两篇文章,但它只打印了第一篇文章。为什么它不打印第二个?很明显,它看到了另一个帖子感谢Thomas,我已经修复了缩进问题。我收到一个通知:试图在C:\xampp\htdocs\base\main3.php的第164行获取非对象的属性,错误在if$stmt->num_rows>0{?这是什么意思..它意味着$stmt失败,您可以检查$conn错误状态,看看查询中是否有错误,我看不出有任何错误…我在代码中唯一更改的是上面提到的sql语句,那么这是否有问题?如果我将其更改回原来的语句,它似乎工作正常。还有p.title和t.total与正常显示不同,它们是升华中的紫色,而不是像操作和变量那样的红/黄颜色。你能给出一点你的posts表要点吗?例如,我可以测试一下。对于颜色,我建议检查生成的html。
SELECT Month, Year, p.title, t.total
FROM posts p 
INNER JOIN (SELECT DISTINCT Year(time) Year, Month(time) Month, SUM(1) FROM posts GROUP BY Year, Month) t ON t.Year = Year(p.time) AND t.Month = Month(p.time)
ORDER BY time DESC