php - 在文件循环中仅显示一次年份

标签 php mysql date loops blogs

我刚刚开始使用 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 &nbsp;'. $get_year . '</dt>';

echo '<dd><a href="archives.php?month='. $get_month .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $entries . ')</dd>';

echo '</dl>';  

}

好的。然后浏览器中的结果看起来类似如下:

  • 2012 年的条目
    1. 一月 (2) 的条目
  • 2012 年的条目
    1. 二月份的条目 (1)

现在我的问题是:如何恢复一年中的所有月份?像这样:

  • 2012 年的条目
    1. 一月 (2) 的条目
    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 &nbsp;'. $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 &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')</dd>';

  }

  echo '</dl>';


}

再次感谢大家!

最佳答案

我发现最简单的方法是这样的:

$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
  $entries[$row['get_year']][] = $row;
}

foreach($entries as $year => $months) {
  echo '<dl>';
  echo '<dt>Entries from &nbsp;'. $year . '</dt>';

  echo '<dd>';
  foreach($months as $month) {\
    $this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );

    echo '<a href="archives.php?month='. $month['get_month'] .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')';
  }
  echo '</dd></dl>';


}

该 HTML 标记并不理想,但您应该明白了。

关于php - 在文件循环中仅显示一次年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9253070/

相关文章:

javascript - 如何在 php 中的 jquery 中对循环内的选择框值求和

mysql - 对MySQL中的多个字段进行排序

java - 在 Scala 中将日期转换为时间戳

javascript - 将字符串日期格式 "Mon Jun 01 2015 00:00:00 GMT+0100 (IST)"更改为 "YYYY-MM-DD"

java - 如何通过 'java.sql.date' 获取当前系统月份?

php - 从 PHP/MySQL 估算数据中检索结果的平均值

php - 插入值列表与列列表不匹配 : 1136 Column count doesn't match value count at row 1

php - 将连接文件包含到类 "Undefined variable conn"

mysql - MySQL中BEGIN和END的正确使用

php - 数据表 : Using row to columns with php/mysql or else?