Yii 分页方法总结

利用CPagination实现

在 Controller中如下代码:

public function actionIndex()
    {
        $criteria = new CDbCriteria();
        $criteria->order = 'createtime desc';    
        $count = Article::model()->count($criteria);
        
        $pager = new CPagination($count);
        $pager->pageSize = 10;         
        $pager->applyLimit($criteria);

        $artList = Article::model()->findAll($criteria);
        $this->render('index',array('pages'=>$pager,'list'=>$artList));
    }

在视图中代码如下:
<div id="pager">
	<?php

	$this->widget('CLinkPager',array(
		'header'=>'',
		'firstPageLabel' => '首页',
		'lastPageLabel' => '末页',
		'prevPageLabel' => '上一页',
		'nextPageLabel' => '下一页',
		'pages' => $pages,
		'maxButtonCount'=>13
		)
	);
	?>
	</div>

ps: 一般写法:
    $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();

$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();

在yii中有自带的分页方式,通过cgridview等

你可能感兴趣的:(sql,list,function,user,header,div)