Yii provides two paging methods.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.