如何在CodeIgniter中显示文章存档


How to show archive of articles in CodeIgniter?

我有一个问题,我还没有找到一个解决方案。我在我的博客中有一个档案,看起来像:

May[2014]
  -Title 1 article
May[2014]
  -Title 2 article
April[2014]
  -Title 3 article

但是我想如果有一个或多个文章在同一个月发表,像这样显示:

May[2014]
 -Title 1 article
 -Title 2 article
May[2014]
 Title 3 article
我代码:

<?php if ($archive): $_month = '0000-00'; ?>
                <?php foreach($archive as $a):?>
                    <div class="archive">
                        <div class="month">
                        <?php if (date('Y-m', strtotime($a['date'])) !== $_month) :?>
                            + <?php echo date('M',strtotime($a['date']))?> [<?php echo date("Y",strtotime($a['date']))?>]
                                <div class="month-article last-article-content">
                                    <span class="most-popular-articles"><img src="<?php echo config_item('base_url');?>assets/images/arrow.png">&nbsp&nbsp<a href="<?php echo base_url('materials/'.$a['id'].'/'.make_permalink($a['title'])).'.html'; ?>"><?php echo ($a['title']) ?></a></span>
                                </div>
                        <?php endif; ?>
                        <?php $_month = date('Y-m',strtotime($a['date'])) ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>

$a['date']+1不会像你想象的那样工作。

您必须创建一个包含前一个日期的变量,假设您的文章是按日期排序的。

<?php 
$_month = '0000-00';
foreach ($articles as $a) : 
    if (date('Y-m', strtotime($a['date'])) !== $_month) {
        echo date('M',strtotime($a['date']));
    }
    ...
    $_month = date('Y-m', strtotime($a['date']));
endforeach; 
?>