Yii provides two paging methods.
Yii implements paging in two ways. One is implemented using DAO, and the other is implemented in widgets.
Each has its own advantages. The first is more efficient, and the second is more convenient.
1. Implement Paging for DAO.
[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 // pagination widget code: $ this-> widget ('clinkpager', array ('pages '=> $ pages);?>
Advantage: High DAO efficiency; disadvantage: The view layer needs to write some styles by itself, which is a little more troublesome.
Ii. Implement Paging for widgets
[Model layer]
/*** @ Var string attribute: daily freight (used for Statistics) * declare the newly added field */public $ dayrate;/** statistical function: collect daily freight */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 (// set the field 'bubuckets' => array ('remitdate ', 'dayrate' => array ('asc '=> 'dayrate', 'desc' => 'dayrate desc ',)), 'defaultorder' => 'remitdate desc ',),));}
[Controller layer]
/*** Waybill statistics function: * collect by date */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', ), ),)); ?>
Advantage: You can use the built-in style. disadvantage: the efficiency is slightly lower.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.