yii分页

【转】yii 分页功能详解


Yii 是利用CPagination来实现

//在 Controller(控制器) 中的代码

public function actionIndex()

{

$criteria = new CDbCriteria();

$criteria->order = ' 数据库字段 desc'; //按什么字段来排序

$count = Article::model()->count('$criteria');//count() 函数计算数组中的单元数目或对象中的属性个数。

$pager = new CPagination('$count');

$pager -> pageSize = 10; //每页显示的行数

$pager->applyLimit($criteria);

$artList = Article::model()->findA($criteria);//查询所有的数据

$this->render('index',array('pages'=>$pager,'list'=>$artList));

}

下面是view中的代码

<div>

<?php

$this->widget('CLinkPager',array(

'header'=>'',

'fristPageLabel'=>'首页',

'lastPageLabel'=>'末页',

'prevPageLabel'=>'上一页',

'nextPageLabel'=>'下一页',

'pages'=>'$pages',

'maxButtonCount'=>13,

)

);

</div>



一般写法:

$criteria = new CDbCriteria; 
$count = DnOnline::model()->count($criteria); 

$pages = new CPagination($count); 
$pages->pageSize = 5; 
$pages->applylimit($criteria); 

$model = DnOnline::model()->findAll($criteria); 

$criteria = new CDbCriteria; 

$count = DnOnline::model()->count($criteria); 

$pages = new CPagination($count); 

$pages->pageSize = 5;

$pages->applylimit($criteria); $model = DnOnline::model()->findAll($criteria); 
CDB写法: 

$criteria = new CDbCriteria; 
$sql = "SELECT * FROM USER"; 
$model= Yii::app()->db->createCommand($sql)->queryAll(); 
$pages = new CPagination(count($model)); 
$pages->pageSize = 4; 
$pages->applylimit($criteria); 

$model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); 
$model->bindValue(':offset', $pages->currentPage*$pages->pageSize); 
$model->bindValue(':limit', $pages->pageSize); 
$model=$model->queryAll(); 

CDB写法: $criteria = new CDbCriteria; 

$sql = "SELECT * FROM USER"; 

$model= Yii::app()->db->createCommand($sql)->queryAll(); 

$pages = new CPagination(count($model)); $pages->pageSize = 4; 

$pages->applylimit($criteria); 

$model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); 

$model->bindValue(':offset', $pages->currentPage*$pages->pageSize); $model->bindValue(':limit', $pages->pageSize); $model=$model->queryAll();
$criteria=new CDbCriteria(); 

$result = Yii::app()->db->createCommand($sql_do)->query(); 

$pages=new CPagination($result->rowCount); 

$pages->pageSize=2; 

$pages->applyLimit($criteria); 

$result=Yii::app()->db->createCommand($sql_do." LIMIT 2"); 

$result->bindValue(':offset', $pages->currentPage*$pages->pageSize); 

$result->bindValue(':limit', $pages->pageSize); 

$list=$result->query();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值