【发布时间】: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,你会看到。
-
虽然它确实列出了九月以下的两个帖子,但它只打印了第一个帖子。为什么它不打印第二个?它显然看到另一个帖子
-
谢谢托马斯,我已经解决了缩进问题。