如何使用Codeigniter活动记录按年和月分组?

时间:2014-11-03 05:40:07

标签: php mysql codeigniter

我想按年份和月份将杂志的问题分组到我的网站上。

我的数据库中有年份和月份字段,我将用它按年份和月份对数据进行分组。 (例如2013年4月)。 我想要得到的例子(由DESC订购):

2014
  December
  November
  October
  ....

2013
  December
  November
  October
  ....

型号:

public function get_all_mag($type){
   $this->db->where('type', $type); 
   $this->db->order_by("mag_year", "desc"); 
   $query = $this->db->get('magazine');
   return $query->result();
}

控制器:

$data['magarchive'] = $this->Mdl_magazine->get_all_mag($id);

查看:

<?php foreach ($magarchive as $archive): ?>
    <div class="arhive_block">
        <span class="year"><?php echo $archive->mag_year; ?></span>
        <div class="ar_nom">
            <a href="<?php echo base_url();?>magazine/issue/<?php echo $archive->id; ?>">
                <img src="<?php echo base_url();?>upload/<?php echo $archive->photo_footer; ?>" alt="">
            </a>
            <a href="<?php echo base_url();?>magazine/issue/<?php echo $archive->id; ?>" class="month"><?php echo $archive->mag_month; ?></a>
        </div>
    </div>
<?php endforeach; ?>

现在,我得到这样的话:

2014
 December
2014
 November
...
2013
 December
2013
 November
...

我不是codeigniter的专家,我刚开始不久,所以请不要拍初学者。非常感谢你。

3 个答案:

答案 0 :(得分:3)

像你所描述的那样分组和显示肯定会有点令人困惑。首先,您需要在$this->db->group_by(array("mag_year", "mag_month"));方法中添加get_all_mag()

public function get_all_mag($type) {
    $this->db->where('type', $type); 
    $this->db->order_by("mag_year", "desc"); 
    $this->db->group_by(array("mag_year", "mag_month"));
    $query = $this->db->get('magazine');
    return $query->result();
}

这样做的结果是按年份分组结果,然后按月分组,这样您就可以获得预期的数据。不幸的是,当你输出数据时,你仍会得到相同的结果,但是如果你更新get_all_mag()方法来返回一个对象数组,键是年份,它将使输出数据更容易

public function get_all_mag($type) {
    $this->db->where('type', $type); 
    $this->db->order_by("mag_year", "desc"); 
    $this->db->group_by(array("mag_year", "mag_month"));
    $query = $this->db->get('magazine');

    $magarchive = array();

    if ( $results = $query->result() ) {
        foreach ( $results as $result ) {
            if ( !isset($mag[$result->mag_year]) ) {
                $magarchive[$result->mag_year] = array();
            }

            $magarchive[$result->mag_year][] = $result;
        }
    }

    return $magarchive;
}

这将返回一个对象数组,而不是一个单数对象。要以您想要的方式输出响应,您必须更改视图以使嵌套循环首先输出年份,然后在该年下每月输出。

<?php foreach ($magarchive as $year => $months): ?>
    <div class="arhive_block">
        <span class="year"><?php echo $year; ?></span>
        <?php foreach ( $months as $archive ) : ?>
            <div class="ar_nom">
                <a href="<?php echo base_url();?>magazine/issue/<?php echo $archive->id; ?>">
                    <img src="<?php echo base_url();?>upload/<?php echo $archive->photo_footer; ?>" alt="">
                </a>
                <a href="<?php echo base_url();?>magazine/issue/<?php echo $archive->id; ?>" class="month"><?php echo $archive->mag_month; ?></a>
            </div>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

答案 1 :(得分:0)

使用:

$this->db->group_by(array("year", "month"));

答案 2 :(得分:0)

使用以下更新的代码。希望这会有所帮助 -

public function getArchiveDates()
{
    $this->db->select('Year(posted_on) as year, MONTHNAME(posted_on) as month,post_id,post_title');
    $this->db->where('status', '1');
    $this->db->order_by("YEAR(posted_on)", "desc");
    $this->db->order_by("MONTH(posted_on)", "desc");
    $this->db->group_by(array("year", "month"));
    $query = $this->db->get('tbl_mst_blog_posts');

    $magarchive = array();

    if ($results = $query->result()) {
        foreach ($results as $result) {

            $magarchive[$result->year][] = $result;
        }
    }   

    return $magarchive;
}

在查看文件中使用以下代码 -

<?php foreach ($archive_dates as $year => $months): ?>
                <div class="arhive_block">
                    <span class="year"><?php echo $year; ?></span>
                    <?php foreach ($months as $archive) : ?>
                        <div class="ar_nom">

                            <a href="<?php echo base_url(); ?>blog/<?php echo $archive->month; ?>" class="month"><?php echo $archive->month; ?></a>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endforeach; ?>