dongpa5277 2012-02-12 21:53
浏览 10
已采纳

too long

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 &nbsp;'. $get_year . '</dt>';

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


}

Thanks to all again!

  • 写回答

3条回答 默认 最新

  • dongxiangshen7916 2012-02-12 22:01
    关注

    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 &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>';
    
    
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题