There are two ways to implement paging in Yii, one is to use DAO, and the other is to implement it in widgets.
Each has its own advantages. The first one is more efficient, and the second one is more efficient. You can use the built-in table, which is more convenient.
1. DAO implements paging.
[Controller layer]
public function actionReport() { $sql = "select remitdate, sum(rate) sumrate from td_delivery group by remitdate order by remitdate desc"; $criteria=new CDbCriteria(); $result = Yii::app()->db->createCommand($sql)->query(); $pages=new CPagination($result->rowCount); $pages->pageSize=2; $pages->applyLimit($criteria); $result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); $result->bindValue(':offset', $pages->currentPage*$pages->pageSize); $result->bindValue(':limit', $pages->pageSize); $posts=$result->query(); $this->render('report',array( 'posts'=>$posts, 'pages'=>$pages, )); }
[View layer]
<?php foreach($posts as $row):?> <?php echo CHtml::link($row["remitdate"],array('delivery/view','remitdate'=>$row["sumrate"]));?> <?php echo $row["sumrate"]."<br />" ?> <?php endforeach;?> <?php //分页widget代码: $this->widget('CLinkPager',array('pages'=>$pages)); ?>
Advantages: DAO is highly efficient; Disadvantages: the view layer needs to write some styles by itself, which is a little troublesome
2. Widget implements paging
[model layer]
/** * @var string attribute : 日运费 (统计用) * 需要对新增加的字段做个声明 */ public $dayrate; /* * 统计功能: 统计每日的运费 */ public function statistics() { $criteria = new CDbCriteria; $criteria->select = 'remitdate, sum(rate) AS dayrate'; $criteria->group = 'remitdate'; return new CActiveDataProvider(get_class($this), array( 'criteria'=>$criteria, 'sort'=>array( // 表头设置点击排序的字段 'attributes'=>array( 'remitdate', 'dayrate'=>array( 'asc'=>'dayrate', 'desc'=>'dayrate DESC', ) ), 'defaultOrder'=>'remitdate desc', ), )); }
[Controller layer]
/** * 运单统计功能: * 按日期统计 */ public function actionReport() { $model=new Delivery('statistics'); $model->unsetAttributes(); // clear any default values $this->render('report',array( 'model'=>$model, )); }
[View layer]
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'delivery-grid', 'dataProvider'=>$model->statistics(), 'filter'=>$model, 'columns'=>array( 'remitdate', 'dayrate', array( 'class'=>'CButtonColumn', ), ), )); ?>
Advantages: You can use your own style; Disadvantages: Slightly less efficient .
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more detailed explanations of the two methods of paging in Yii, please pay attention to the PHP Chinese website for related articles!