Yii-custom archiving in the blog system

Source: Internet
Author: User
Hello everyone, I have implemented archive management for a blog system, which can manage tbl_post. I only used CDbCriteria in a simple way. although there are still many better ways to do this, but I think that by modifying or customizing my code, this can create some inspiration for users.

Hello everyone, I have implemented archive management for a blog system, which can manage tbl_post. I only used CDbCriteria in a simple way. although there are still many better ways to do this, but I think that by modifying or customizing my code, this can create some inspiration for users.

Define in the module

public $year; public $month;

The following code can be used in the controller view:

 'January','2'=>'Feburary','3'=>'March','4'=>'April','5'=>'May','6'=>'June','7'=>'July','8'=>'August','9'=>'September','10'=>'October','11'=>'November','12'=>'December'); $condition = new CDbCriteria; $condition->select='YEAR(t.createddate) as year'; //declare year in post model model $condition->distinct=true; $condition->order ='createddate DESC'; $yeardataarticle = Post::model()->findAll($condition); foreach ($yeardataarticle as $ya): $year =$ya->year; echo '
'; echo $year; $monthcriteria = new CDbCriteria; $monthcriteria->select ='MONTH(t.createddate) as month'; // declare month in post model $monthcriteria->condition ='YEAR(t.createddate)=:year'; $monthcriteria->params=array(':year'=>$year); $months = Post::model()->findAll($monthcriteria); foreach ($months as $month): $montth = $month->month; echo '
'; foreach($monthsvalue as $key=>$value): if (!isset(${'printed'.$key.$year})) ${'printed'.$key.$year} = false; //var_dump('printed'.$key.$year);exit; if($key==$montth and !${'printed'.$key.$year}){ echo CHtml::link($value,array('//post/archivepost','year'=>$year,'month'=>$montth)); ${'printed'.$key.$year}=true; $postcount = new CDbCriteria; $postcount->addCondition('MONTH(t.createddate)=:month AND YEAR(t.createddate)=:year'); $postcount->params = array(':month'=>$montth,':year'=>$year); $count = Post::model()->count($postcount); echo ' ('.$count.')'; } endforeach; endforeach; endforeach; ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.