首页 > 代码库 > Yii实现分页的两种方法
Yii实现分页的两种方法
一. DAO实现分页.
[Controller层]
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层]
<?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层需要自己写一些样式,稍显麻烦一点
二. widget实现分页
[model层]
/** * @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层]
/** * 运单统计功能: * 按日期统计 */ public function actionReport() { $model=new Delivery(‘statistics‘); $model->unsetAttributes(); // clear any default values $this->render(‘report‘,array( ‘model‘=>$model, )); }
[VIEW]层
<?php $this->widget(‘zii.widgets.grid.CGridView‘, array( ‘id‘=>‘delivery-grid‘, ‘dataProvider‘=>$model->statistics(), ‘filter‘=>$model, ‘columns‘=>array( ‘remitdate‘, ‘dayrate‘, array( ‘class‘=>‘CButtonColumn‘, ), ), )); ?>
Yii实现分页的两种方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。