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.