在档案循环中仅显示一次年份
我刚刚开始使用 PHP 和 mySQL,并且正在创建某种博客。有时我认为一切进展顺利 - 我只是在我的档案代码中遇到了一些问题。
我认为解决方案非常简单,但我太盲目了,我自己找不到重点。
这是我的实际代码,一切正常,链接也是:
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_month ORDER BY date ASC") or die('No response from database.');
while ($row = mysql_fetch_array($sql)) {
$get_year = $row["get_year"];
$get_month = $row["get_month"];
$entries = $row["entries"];
// get month name
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );
echo '<dl>';
echo '<dt>Entries from '. $get_year . '</dt>';
echo '<dd><a href="archives.php?month='. $get_month .'">Entries from '. $this_month . ' </a>(' . $entries . ')</dd>';
echo '</dl>';
}
好的。然后浏览器中的结果看起来类似如下:
- Entries from 2012
- 一月 (2) 的条目
- 2012 年的条目
- 二月份的条目 (1)
现在我的问题是:如何恢复一年中的所有月份?像这样:
- 2012 年的条目
- 一月 (2) 的条目
- 二月份的条目 (1)
我害怕创建一个 12 个月的数组,因为可能不是每个月都会有条目,而且我不想显示空月份。
有人可以帮助我吗?谢谢!!
--------------
在这里,我再次展示您友好帮助的最终(工作!!)结果:
至少,我使用了 Sam 的版本,因为它正是为了我正在寻找的东西。我真的很感激其他答案 - 特别是因为现在我有更多的东西要考虑下一步的尝试。
Sam 的代码运行得非常棒……我遇到的唯一问题是,在使用该数组后,它只将“十二月”打印为月份。
于是我又看了一遍代码,经过2个小时的寻找和尝试,我找到了问题所在。它嵌套在以下行中:
$this_month = date( 'F', mktime(0, 0, 0, $row['get_month']) );
将其更改为:
$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
现在一切正常。正如我所期望的那样。这就是最终的工作代码:
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_year, get_month ORDER BY date ASC") or die('No response from database.');
$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
$entries[$row['get_year']][] = $row;
}
foreach($entries as $year => $months) {
echo '<dl>';
echo '<dt>Entries from '. $year . '</dt>';
foreach($months as $month) {
$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
echo '<dd><a href="archives.php?month='. $month['get_month'] .'">Entries from '. $this_month . ' </a>(' . $month['entries'] . ')</dd>';
}
echo '</dl>';
}
再次感谢大家!
I just started with PHP and mySQL and I'm creating some kind of blog. At time I think it's going well - I just got some trouble with my archives code.
I think the solution will be pretty easy but I'm so blinded that I just can't find the point on my own.
This is my actual code and everything works fine, also the links:
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_month ORDER BY date ASC") or die('No response from database.');
while ($row = mysql_fetch_array($sql)) {
$get_year = $row["get_year"];
$get_month = $row["get_month"];
$entries = $row["entries"];
// get month name
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );
echo '<dl>';
echo '<dt>Entries from '. $get_year . '</dt>';
echo '<dd><a href="archives.php?month='. $get_month .'">Entries from '. $this_month . ' </a>(' . $entries . ')</dd>';
echo '</dl>';
}
Ok. Then the result in the browser looks similar like this:
- Entries from 2012
- Entries from January (2)
- Entries from 2012
- Entries from February (1)
Now my question: How can I resume all months inside the year? Like this:
- Entries from 2012
- Entries from January (2)
- Entries from February (1)
I'm scared about creating a 12-months array cause maybe not every month there will be entries, and I don't want to show empty months.
Can someone help me? Thanks!!
---------------
Here I am again to show the final (working!!) result of your friendly help:
At least, I used Sam's version cause it was just for what i was looking for. I really appreciate the other answers - specially 'cause now I have more stuff to think about for the next tries.
Sam's code worked just awesome... the only trouble I had was, that after using the array, it only printed out 'December' as months.
So I looked over again the code and after 2 hours of seek and try, I found the point of trouble. It was nested in the following line:
$this_month = date( 'F', mktime(0, 0, 0, $row['get_month']) );
Changing it (It's logic, but for me as greenhorn it toke me a while) to:
$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
Now everything work fine. Just how I expected. So this is the working final code:
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_year, get_month ORDER BY date ASC") or die('No response from database.');
$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
$entries[$row['get_year']][] = $row;
}
foreach($entries as $year => $months) {
echo '<dl>';
echo '<dt>Entries from '. $year . '</dt>';
foreach($months as $month) {
$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
echo '<dd><a href="archives.php?month='. $month['get_month'] .'">Entries from '. $this_month . ' </a>(' . $month['entries'] . ')</dd>';
}
echo '</dl>';
}
Thanks to all again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现最简单的方法是这样的:
HTML 标记并不理想,但您应该明白这一点。
I find the simplest way is something along these lines:
That HTML markup isn't ideal, but you should get the idea.
首先将您的查询更改为按年和月分组,而不仅仅是月份:
我不记得旧的 mysql API,所以这里的代码为 PDO (未经测试,但在修复潜在的拼写错误后应该可以工作)
您还可以尝试使用 PDO::FETCH_GROUP 来简化代码,如中所述
fetchAll 方法文档 但我从未尝试过这个,所以我不会给你代码。
First of all change your query to group by year and month, not only month:
I don't remember old mysql API, so here's code with PDO (untested, but should work after fixing potential typos)
You could also try to simplify code by using PDO::FETCH_GROUP, as described in
fetchAll method doc but I never tried this one, so I won't give you code.
试试这个
try this