2014-11-03 37 views
2

我想按年份和月份將雜誌的問題分組在我的網站上。如何使用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; ?> 

現在,我越來越像t他:

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

我不是在笨的專家,我剛開始不是很久以前的事,所以請不要拍初學者。非常感謝。

回答

2

分組並顯示,就像你描述可以肯定是有點混亂。首先,您需要將$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; ?> 
+0

謝謝!但它只顯示每年的第一個月的問題。這樣:2014 - > January,2013-> January。每年的一個結果 – Refresh 2014-11-03 06:47:28

+0

更正了你的代碼並使其正常工作!謝謝! – Refresh 2014-11-03 06:57:32

+0

很高興能幫到你! – zachflower 2014-11-03 16:20:54

0

用途:

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

+0

如何將它們分組,同時取? – Refresh 2014-11-03 06:33:35

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