简体   繁体   中英

How to group by year and month with Codeigniter active record?

I want to group the magazine's issues on my web-site by year and month.

I have year and month fields in my database that I will use to group my data by year and month. ( example 2013, April). Example of what I want to get (Ordered By DESC):

2014
  December
  November
  October
  ....

2013
  December
  November
  October
  ....

Model:

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();
}

Controller:

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

View:

<?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; ?>

Now, I'm getting sth like this:

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

I'm not an expert in codeigniter, I just started not long time ago, so please don't shoot the beginner. Thank you so much.

Grouping and displaying like you've described can definitely be a bit confusing. First, you'll want to add a $this->db->group_by(array("mag_year", "mag_month")); to the get_all_mag() method.

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();
}

What this will do is group the results by year, and then by month, so you'll get the data you are expecting. Unfortunately, you'll still get the same results when you output the data, but if you update the get_all_mag() method to return an array of objects, with the keys being the year, it'll make outputting the data much easier.

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;
}

This will return an array of objects, rather than a singular object. To output the response in the way you want, you'll have to change your view to have a nested loop to output first the year, and then each month under that year.

<?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; ?>

采用:

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

Use below updated code. Hope this will help -

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;
}

Use below code in you view file -

<?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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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