在档案循环中仅显示一次年份

发布于 2025-01-04 16:42:04 字数 2491 浏览 1 评论 0原文

我刚刚开始使用 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>';  

}

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

  • Entries from 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>';


}

再次感谢大家!

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
    1. Entries from January (2)
  • Entries from 2012
    1. Entries from February (1)

Now my question: How can I resume all months inside the year? Like this:

  • Entries from 2012
    1. Entries from January (2)
    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街女流氓 2025-01-11 16:42:04

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

$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>';

  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  '. $this_month . ' </a>(' . $month['entries'] . ')';
  }
  echo '</dd></dl>';


}

HTML 标记并不理想,但您应该明白这一点。

I find the simplest way is something along these lines:

$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>';

  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  '. $this_month . ' </a>(' . $month['entries'] . ')';
  }
  echo '</dd></dl>';


}

That HTML markup isn't ideal, but you should get the idea.

我的痛♀有谁懂 2025-01-11 16:42:04

首先将您的查询更改为按年和月分组,而不仅仅是月份:

$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"

我不记得旧的 mysql API,所以这里的代码为 PDO (未经测试,但在修复潜在的拼写错误后应该可以工作)

$pdo = new PDO('mysql:host=localhost;dbname=db', 'user', 'password');
$stmt = $pdo->prepare($query);
$stmt->execute();

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

//group results by year *
$groupped = array();
foreach ($results as $record) {
    $groupped[$record['get_year']] = $record;
}

//print results
echo "<dl>";
foreach ($groupped as $year => $monthsRecords) {
    echo "<dt>$year</dt>";
    foreach ($monthsRecords as $record) {
        echo "<dd>{$record['get_month']} : {$record['entries']}</dd>";
    }
} 
echo "</dl>";

您还可以尝试使用 PDO::FETCH_GROUP 来简化代码,如中所述
fetchAll 方法文档 但我从未尝试过这个,所以我不会给你代码。

First of all change your query to group by year and month, not only month:

$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"

I don't remember old mysql API, so here's code with PDO (untested, but should work after fixing potential typos)

$pdo = new PDO('mysql:host=localhost;dbname=db', 'user', 'password');
$stmt = $pdo->prepare($query);
$stmt->execute();

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

//group results by year *
$groupped = array();
foreach ($results as $record) {
    $groupped[$record['get_year']] = $record;
}

//print results
echo "<dl>";
foreach ($groupped as $year => $monthsRecords) {
    echo "<dt>$year</dt>";
    foreach ($monthsRecords as $record) {
        echo "<dd>{$record['get_month']} : {$record['entries']}</dd>";
    }
} 
echo "</dl>";

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.

暖阳 2025-01-11 16:42:04

试试这个

$start_year =  date("Y")-10;
$end_year = date("Y");
for ($i=$start_year;$i<$end_year;$i++){
 $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*)    AS entries FROM blogdata where YEAR(date) = $i GROUP BY get_month ORDER BY date ASC"); or die('No response from database.');
   //execute your query here. get the 
    $result = mysql_query($sql);
      echo '<dl>';
    echo '<dt>Entries from  '. $i . '</dt>';

   while ($row = mysql_fetch_array($result )) {

       $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 '<dd><a href="archives.php?month='. $get_month .'">Entries from  '.   $this_month .  ' </a>(' . $entries . ')</dd>';

 }
   echo '</dl>';
}

try this

$start_year =  date("Y")-10;
$end_year = date("Y");
for ($i=$start_year;$i<$end_year;$i++){
 $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*)    AS entries FROM blogdata where YEAR(date) = $i GROUP BY get_month ORDER BY date ASC"); or die('No response from database.');
   //execute your query here. get the 
    $result = mysql_query($sql);
      echo '<dl>';
    echo '<dt>Entries from  '. $i . '</dt>';

   while ($row = mysql_fetch_array($result )) {

       $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 '<dd><a href="archives.php?month='. $get_month .'">Entries from  '.   $this_month .  ' </a>(' . $entries . ')</dd>';

 }
   echo '</dl>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文