Use of paging function in Yii

Source: Internet
Author: User

Cdbcriteria is the use of this class of packages, which are designed to be used by YII to handle functions like classification. And we use the YII framework and then call this method will play a multiplier effect, will find that the use of this can save a lot of time, let you quickly use PHP in the function of paging.

Also to use a class package is cpagination, the specific element attributes are not explained, you can directly connect to the past to see the API

The first step:

Add a bit of code to the controller:

$criteria = new Cdbcriteria (); $criteria->alias = ' post ';//To query the alias of the table $criteria->order = ' createtime desc '; The length of the list time shown in the article $criteria->condition = ' type_id=:type_id ';//The condition for querying records is the same as the use of Find () in Yii $criteria->pa        Rams = Array (': type_id ' = = $id);        $criteria->distinct = ";//is the distinct value setting in SQL query statement, generally use less $criteria->group =";//is the value setting of group in SQL query statement, generally use less $criteria->select = ' * ';//sql the SELECT statement in the query statement is worth setting, the default is * $count = Post::model ()->count ($criteria);// By setting the above query statement, and then querying out the number of records that meet the above criteria $pages = new Cpagination ($count);//determine how many pages//cpagination the above record can be displayed/to render relevant information to the page number. When the data needs to be rendered to multiple pages, we may use cpagination to render the information, such as total item count, page Size,current page, and so on.         This information is passed to the pagers Render paging button or link.        $pages->pagesize = 1;//The record of each page shows how many $pages->applylimit ($criteria); $post = Post::model ()->findall ($criteria);

Step Two:

Just add a bit of code in the views display, you can show the effect.

<div class= "Pagenav" id= "Pagenavi" ><span> total <?php echo $pages->itemcount;?> page </span><? Php$this->widget (' Clinkpager ',         array (                ' header ' = = ', '                firstpagelabel ' = ' home ',                ' Lastpagelabel ' + ' last ',                ' prevpagelabel ' = ' prev ', ' Nextpagelabel ' and '                Next ',                ' pages ' and $pages,                ' maxbuttoncount ' = 8        ));? ></div>
If you use the bootstrap framework, you can use the following code to show the bootstrap effect.

<!--//Use bootstrap style paging--><?php    $this->widget (' Clinkpager ', Array (    ' pages ' = $pages,    ' Header ' =>false,    ' htmloptions ' =>array (' class ' = ' pagination pull-right '),    ' Selectedpagecssclass ' = ' active ',    ' hiddenpagecssclass ' = ' disabled ',    ' firstpagelabel ' = ' home ',    ' lastpagelabel ' = > ' last ',    ' prevpagelabel ' = ' «',    ' nextpagelabel ' = '» ', '    maxbuttoncount ' =>5,    ' Cssfile ' =>false,    ' firstpagecssclass ' = ' previous ',    ' lastpagecssclass ' = ' next ',    ); >

Here is the effect of bootstrap:



Use of paging function in Yii

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.