在档案循环中只显示一次 [英] Show year only once in archives loop

查看:131
本文介绍了在档案循环中只显示一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从PHP和mySQL开始,我正在创建一些博客。当时我认为这很顺利 - 我只是遇到了一些我的档案代码的麻烦。



我认为这个解决方案会很容易,但我很失明,我只是无法找到我自己的点。



这是我的实际代码,一切正常,还有链接:


$ b $ ($)$ sql = mysql_query(SELECT YEAR(date)AS get_year,MONTH(date)AS get_month,COUNT(*)AS条目FROM blogdata GROUP BY get_month ORDER BY date ASC )或死(数据库没有响应);


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

$ get_year = $ row [get_year];
$ get_month = $ row [get_month];
$ entries = $ row [entries];

//获取月份名称
$ this_month = date('F',mktime(0,0,0,$ row [get_month]));



echo'< dl>';
echo'< dt>来自& nbsp;'的条目。 $ get_year。 '< / dt>';

echo'< dd>< a href =archives.php?month ='。$ get_month。'>来自& nbsp;'的条目。 $ this_month。 '& nbsp;< / a>('。$ entries。')< / dd>';

echo'< / dl>';

}

好的。然后浏览器中的结果看起来像这样:




  • 从2012


    1. 1月(2)的条目


  • 从2012年起的参赛作品


    1. 1)




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




  • 从2012


    1. 从1月(2) li>
    2. 从二月(1)起的条目




我害怕创建一个12个月的阵容,可能不是每个月都会有条目,我不想显示空的月份。



有人可以帮助我?谢谢!!



---------------



这里我再次显示你的友好帮助的最终(工作!!)结果:



至少我使用了Sam的版本,因为它只是为了我正在寻找。我真的很感激其他答案 - 特别是因为现在我有更多的东西要考虑下一次尝试。



山姆的代码工作只是真棒...唯一的麻烦我曾经是,在使用阵列后,它只打印出十二月为几个月。



所以我再次看了一遍代码,经过2个小时的寻求和尝试,我发现了麻烦点。它被嵌套在以下行中:

  $ this_month = date('F',mktime(0,0,0,$ row ['get_month'])); 

更改它(这是逻辑,但是对于我来说, p>

  $ 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条目FROM blogdata GROUP BY get_year,get_month ORDER BY date ASC)或死(数据库没有响应); 


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

foreach($ entries as $ year => $ months){
echo'< dl>';
echo'< dt>来自& nbsp;'的条目。 $年。 '< / 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']。'>来自& 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>来自& nbsp;'的条目。 $年。 '< / 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']。'>来自& nbsp;'的条目。 $ this_month。 '& nbsp;< / a>('。$ month ['entries']。')';
}
echo'< / dd>< / dl>';


}

该HTML标记不理想,但你应该得到这个想法。


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!

解决方案

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.

这篇关于在档案循环中只显示一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆