首页 > PHP资讯 > PHP培训技术 > yii-定制博客系统中的归档

yii-定制博客系统中的归档

PHP培训技术

大家好,我做了一个博客系统的归档管理,它可以管理tbl_post,我仅仅只用到了简单的CDbCriteria,虽然还有很多更好的方法来做这个,但我认为通过修改或定制我的代码这个可以给用户创造一些灵感。

在模块中定义

public $year; public $month;

在控制器的视图中可以使用如下代码:

 $monthsvalue = array('1'=>'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; ?>

PHP培训技术

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持37不支持0