简体   繁体   English

按年和月归档

[英]Archive by year and month

I'm trying to build a year\\month archive. 我正在尝试建立一个年\\月存档。

It refuses to output what i want so i'm hoping one of you might push me in the right direction. 它拒绝输出我想要的内容,所以我希望你们中的一个可以将我推向正确的方向。

I want to sort by year, and show the months for each year ( if exists) and output it like this: 我想按年份排序,并显示每年的月份(如果存在),并输出如下:

2012
 - June
 - August 
2011
 - July

my code is this : 我的代码是这样的:

$query = "SELECT * FROM article WHERE full_name ='$safe_name' group by year, month";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$year = $row['year'];
$month = $row['month'];
echo "<ul class='year'><li><a>{$year}</a>";
echo "<ul class='months'><li><a>{$month}</a></li></ul>
</li></ul>";
}

But it outputs : 但是它输出:

2012
 - june

2012
 - august

2011
 - july

When i group only by year, it will output: 当我仅按年份分组时,它将输出:

2012
 - june
2011
 - july

i have a "date_posted" row which is datetime (yyyy-mm-dd 00:00:00). 我有一个“ date_posted”行,它是日期时间(yyyy-mm-dd 00:00:00)。 On top of that i have 1 row for month and 1 for year ( i know that's dumb, but i couldnt figure out how to do this by just using the "date_posted" row. 最重要的是,我每个月有1行,每年有1行(我知道这很愚蠢,但是我无法通过仅使用“ date_posted”行来弄清楚该如何做。

Ive read up on some posts on this topic, but its not doing the trick for me. 我已经阅读了有关该主题的一些帖子,但对我而言却不足。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

You need to use a simple 'state' machine to detect when a year changes: 您需要使用简单的“状态”机来检测一年的变化时间:

$previous_year = null;
$frist = true;
echo "<ul>"
while($row = mysql_fetch_assoc($result)) {
   if ($row['year'] != $previous_year) {
     // got a new year
     if (!$frist) {
        echo "</ul></li>"; // only close a previous list if there IS a previous list
        $frist = false;
     }
     echo "<li class="year">$row[year]\n<ul>";
     $previous_year = $row['year'];
   }
   echo "<li class="month">$row['month']</li>";
}
$temp_year = 0;
while($row = mysql_fetch_assoc($result)) {
    $year = $row['year'];
    $month = $row['month'];
    if( $temp_year != $year )
    {
        if( $temp_year > 0 )
            echo "</li></ul>";
        $temp_year = $year;
        echo "<ul class='year'><li><a>{$year}</a>";
    }
    echo "<ul class='months'><li><a>{$month}</a></li></ul>";
}

This should work. 这应该工作。 When fetching results, sometimes it's handy to restructure it to be easily usable in iterating a view. 在获取结果时,有时很方便地对其进行重组,以使其易于用于迭代视图。

$query = "SELECT * FROM article WHERE full_name ='$safe_name' group by year, month";
$result = mysql_query($query) or die(mysql_error());

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

   // Get data
   $year = $row['year'];
   $month = $row['month']

   // Structure it
   $archive[$year][] = $month;

}

// Display data
foreach($archive as $year => $months)
{
   // Show year
   echo "<ul class='year'><li><a>{$year}</a>";

   // Month container
   echo "<ul class='months'>"

   // Get months
   foreach($months as $month)
   {
     echo("<li><a>{$month}</a></li>"; 
   }

   // Close Month/Year containers
   echo("</ul></li></ul>");
}

Give a shot 试一把

$storage_array = array();
while($row = mysql_fetch_assoc($result)) {
   $year = $row['year'];
   $month = $row['month'];
   $storage_array[$year][] = $month;
}
foreach ($storage_array as $year => $month_array){
   echo "<ul class='year'><li><a>{$year}</a>";
   foreach ($month_array as $month){
      echo "<ul class='months'><li><a>{$month}</a></li></ul>";
   }
   echo "</li></ul>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM