一. DAO實現分頁.php
[Controller層]sql
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層]app
<?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)); ?>
優勢: DAO效率高; 缺點: view層須要本身寫一些樣式,稍顯麻煩一點ide
二. widget實現分頁post
[model層]this
/** * @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層]spa
/** * 運單統計功能: * 按日期統計 */ public function actionReport() { $model=new Delivery('statistics'); $model->unsetAttributes(); // clear any default values $this->render('report',array( 'model'=>$model, )); }
[VIEW]層
code
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'delivery-grid', 'dataProvider'=>$model->statistics(), 'filter'=>$model, 'columns'=>array( 'remitdate', 'dayrate', array( 'class'=>'CButtonColumn', ), ), )); ?>