Analysis of yii Framework Using Paging Method

  • 2021-12-13 16:27:02
  • OfStack

This article illustrates how the yii framework uses paging. Share it for your reference, as follows:

Paging is convenient in yii in two ways:

In the controller:

1.


$criteria = new CDbCriteria();   //new cdbcriteria Database <br>$criteria->id = 'id ASC';      // Collation rule 
$count = Exchange::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria);
$categoryInfo = Category::model()->findAll($criteria); // Query by Criteria 

2.


$criteria = new CDbCriteria();
$criteria->order = 'id ASC';
$criteria->addCondition('status=1');   // Query by Criteria 
$criteria->addCondition('exchange_status=0');
$count = Exchange::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria); 
$exchangeInfo = Exchange::model()->findAll($criteria);

Parameters passed in render:


array("pages" => $pager)

Add:


$this->widget('CLinkPager',array(
        'header'=>'',
        'firstPageLabel' => ' Home page ',
        'lastPageLabel' => ' Last page ',
        'prevPageLabel' => ' Upper 1 Page ',
        'nextPageLabel' => ' Under 1 Page ',
        'pages' => $pages,
        'maxButtonCount'=>8,
     )
 );

Paging idea:

1. Calculate the total number of items in the database

2. Page size

3. Set the offset limit

In Yii, it is important to use this class CDBcritria for database queries when paging, so paging is simple.

For more readers interested in Yii related contents, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial of Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Usage Summary of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Yii framework.


Related articles: