【问题标题】:MySQL PHP: Blog Post Archive Drop Down MenuMySQL PHP:博客文章存档下拉菜单
【发布时间】:2015-02-04 19:37:56
【问题描述】:

我是 MySQL 和 PHP 新手,只是有几个问题。

目前我正在尝试为在我的 MySQL 数据库中查找的名为“posts”的帖子创建一个存档菜单。

它应该看起来像,

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();
 ?> 

谢谢!

【问题讨论】:

  • 你有什么问题?
  • 我猜你只是错过了关闭你的 HTML 列表。使用编辑器缩进生成的 HTML,你会看到。
  • 虽然它确实列出了九月以下的两个帖子,但它只打印了第一个帖子。为什么它不打印第二个?它显然看到另一个帖子
  • 谢谢托马斯,我已经解决了缩进问题。

标签: php mysql archive


【解决方案1】:

查看您的查询,您按年份分组,因此不显示第二个标题是正常的,并且您正在尝试计算年数。我建议混合一个子查询,这样你就可以得到两个结果,所以改变

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

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

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

验证 $currentYear 和 $currentMonth,如果它们不为零,则必须在打开新的之前关闭。

【讨论】:

  • 我收到“通知:尝试在第 164 行获取 C:\xampp\htdocs\base\main3.php 中非对象的属性”错误,位于“if ($stmt->行数 > 0) {" ?这是什么意思..
  • 表示$stmt失败了,你能检查一下$conn错误状态,看看是不是查询有问题,我看不出有什么错误...
  • 我在代码中唯一改变的是上面提到的sql语句,那有什么问题吗?如果我把它改回旧的,它似乎工作正常。
  • 还有 p.title 和 t.total 的显示方式与正常情况不同,它们是 sublime 中的紫色,而不是像操作和变量那样的红色/黄色..
  • 你能给你的帖子表一些转储(例如要点),所以我可以测试。对于颜色,我建议检查生成的 html。
猜你喜欢
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
相关资源
最近更新 更多