Two ways to implement paging in a detailed yii

Source: Internet
Author: User
Tags php foreach
Yii implementation of two methods of paging, one is implemented in DAO, and the other is implemented in widgets.

Each has the merit, the first kind of efficiency will be higher, the second kind can use the self-brought form, is convenient.

One. DAO implements paging.

[Controller layer]

Public Function Actionreport ()  {    $sql = ' Select Remitdate, sum (rate) Sumrate from Td_delivery       GROUP by REMITD Ate       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));? >

Advantages: DAO efficiency is high; Cons: The view layer needs to write some of its own style, a little more trouble

Two. Widgets for paging

[Model Layer]

/**   * @var string attribute: Daily shipping (statistical)   * requires a declaration of the newly added field */   public  $dayrate;    /   * Statistical function: Statistics 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 (        //table header set Click on the Sort field        ' Attributes ' =>array (          ' remitdate ',          ' dayrate ' =>array (            ' asc ' = ') Dayrate ',            ' desc ' = ' dayrate desc ', '),        ' defaultorder ' = ' remitdate desc ',      ),    ));    }

[Controller layer]

/**   * waybill statistics function:   * Statistics by date */public  function Actionreport ()  {    $model =new Delivery (' statistics ') ;    $model->unsetattributes (); Clear any default values          $this the->render (' report ', Array (      ' model ' = $model,    ));  }

[View Layer]

<?php $this->widget (' Zii.widgets.grid.CGridView ', array (  ' id ' = ' delivery-grid ',  ' Dataprovider ' + $model->statistics (),  ' filter ' and ' = ' $model,  ' columns ' =>array (    ' Remitdate ',    ' dayrate ',    Array (      ' class ' = ' Cbuttoncolumn ',    ),)  ,?>

Advantages: You can use your own style; Cons: Slightly less efficient.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

Related Article

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.