CodeIgniter-每月档案

CodeIgniter-每月档案,codeigniter,Codeigniter,我正试图为我的网站的博客建立一个简单的每月档案列表。我已经从数据库中提取了记录,现在我只需要对它们进行相应的排序,以便它们以如下方式显示: <h4>February '12</h4> <ul> <li>Article 1</li> <li>Article 1</li> <li>Article 1</li> </ul> <h4>Januar

我正试图为我的网站的博客建立一个简单的每月档案列表。我已经从数据库中提取了记录,现在我只需要对它们进行相应的排序,以便它们以如下方式显示:

<h4>February '12</h4>

<ul>
    <li>Article 1</li>
    <li>Article 1</li>
    <li>Article 1</li>
</ul>

<h4>January '12</h4>

<ul>
    <li>Article 1</li>
    <li>Article 1</li>
    <li>Article 1</li>
</ul>
然后,我使用$archives将这些结果传递给我的视图。date_published值返回时间戳,例如2012-01-13 18:39:53


最好的方法是什么?

因为您已经按照时间顺序对它们进行了排序,您可以在循环中遍历它们,并在日期的年-月部分每次更改时打印标题

例如:

// get list of blog-items
$archives = $this->get_archives();

$last_year_month = "";

// traverse items in foreach loop
foreach($archives as $d) {
  // extract year and month
  $year_month = substr($d["date_published"], 0, 7);

  // if year and month has changed
  if ($year_month != $last_year_month) {
    // if not the first heading, close previous UL
    if ($last_year_month != "") echo "</ul>\n";

    $last_year_month = $year_month;

    echo "<h4>" . date("M", mktime(0, 0, 0, intval(substr($year_month, 5, 2)) - 1, 1, 1970)) . " " . substr($year_month, 2, 2) . "</h4>\n";

    // opening UL for next list
    echo "<ul>\n";
  }

  echo "  <li>" . $d["title"] . "</li>\n";
}
// only print if an UL was opened
if ($last_year_month != "") echo "</ul>\n";
//获取博客项目列表
$archives=$this->get_archives();
$last_year_month=“”;
//遍历foreach循环中的项
foreach($d){
//提取年份和月份
$year\u month=substr($d[“发布日期”],0,7);
//如果年份和月份发生变化
如果($year\u month!=$last\u year\u month){
//如果不是第一个标题,请关闭上一个标题
如果($last\u year\u month!=“”)echo“\n”;
$last_year_month=$year_month;
echo.“日期(“M”,mktime(0,0,0,intval(substr($year\u-month,5,2))-1,1,1970))。”substr($year\u-month,2,2)。“\n”;
//为下一个列表打开UL
回声“
    \n”; } 回声“
  • ”$d[“title”]。“
  • \n”; } //仅在UL打开时打印 如果($last\u year\u month!=“”)echo“
\n”;
因为您已经按照时间顺序对它们进行了排序,所以您可以在循环中遍历它们,并在日期的年-月部分每次更改时打印标题

例如:

// get list of blog-items
$archives = $this->get_archives();

$last_year_month = "";

// traverse items in foreach loop
foreach($archives as $d) {
  // extract year and month
  $year_month = substr($d["date_published"], 0, 7);

  // if year and month has changed
  if ($year_month != $last_year_month) {
    // if not the first heading, close previous UL
    if ($last_year_month != "") echo "</ul>\n";

    $last_year_month = $year_month;

    echo "<h4>" . date("M", mktime(0, 0, 0, intval(substr($year_month, 5, 2)) - 1, 1, 1970)) . " " . substr($year_month, 2, 2) . "</h4>\n";

    // opening UL for next list
    echo "<ul>\n";
  }

  echo "  <li>" . $d["title"] . "</li>\n";
}
// only print if an UL was opened
if ($last_year_month != "") echo "</ul>\n";
//获取博客项目列表
$archives=$this->get_archives();
$last_year_month=“”;
//遍历foreach循环中的项
foreach($d){
//提取年份和月份
$year\u month=substr($d[“发布日期”],0,7);
//如果年份和月份发生变化
如果($year\u month!=$last\u year\u month){
//如果不是第一个标题,请关闭上一个标题
如果($last\u year\u month!=“”)echo“\n”;
$last_year_month=$year_month;
echo.“日期(“M”,mktime(0,0,0,intval(substr($year\u-month,5,2))-1,1,1970))。”substr($year\u-month,2,2)。“\n”;
//为下一个列表打开UL
回声“
    \n”; } 回声“
  • ”$d[“title”]。“
  • \n”; } //仅在UL打开时打印 如果($last\u year\u month!=“”)echo“
\n”;
完美!非常感谢你!最后一个问题。如何将例如2012-02转换为2012年2月?我已相应地编辑了答案。(请测试它是否工作。)非常非常接近。我不得不稍微调整一行。“回声”。日期(“F”,mktime(0,0,0,intval(substr($year_-month,5,2))-1970年1月1日)。" " . "'" . substr($年/月,2,2)。""; 非常感谢你的帮助。你真是天赐之物!完美的非常感谢你!最后一个问题。如何将例如2012-02转换为2012年2月?我已相应地编辑了答案。(请测试它是否工作。)非常非常接近。我不得不稍微调整一行。“回声”。日期(“F”,mktime(0,0,0,intval(substr($year_-month,5,2))-1970年1月1日)。" " . "'" . substr($年/月,2,2)。""; 非常感谢你的帮助。你真是天赐之物!