ThinkPHP paging implementation _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP paging implementation. The paging class needs to be combined with the query. we can use the limit method or page method that comes with ThinkPHP, the purpose is to obtain the data on the current page (you can also obtain the complete data first and then combine the paging class with the query. we can use the limit method or page method that comes with ThinkPHP, the purpose is to obtain the data on the current page (you can also obtain the complete data first and then display the data on the front-end by page, which is not described in this article and is not recommended ). The use of the limit or page method is irrelevant to the database type.
We first create a think_datas data table in the database for testing:

Create table if not exists 'think _ data '(
'Id' smallint (4) unsigned not null AUTO_INCREMENT,
'Title' varchar (255) not null,
'Content' varchar (255) not null,
'Create _ time' int (11) unsigned not null,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8;

To use paging query, generally two queries are required, that is, the first query obtains the total data volume that meets the conditions, and then the second query queries the data on the current page, the purpose of this operation is to tell the current total number of data in the paging class to calculate the total number of pages generated (if you only need to flip pages up or down, the total number of queries can be omitted or cached ).
An example of standard paging is as follows:


$ Data = M ('data'); // instantiate a Data object

Import ('org. Util. page'); // import the paging class
$ Count = $ Data-> where ($ map)-> count (); // The total number of records that meet the query requirements. $ map indicates the query conditions.
$ Page = new Page ($ count); // The total number of incoming records in the instantiated paging class
$ Show = $ Page-> show (); // display the output by Page
// Query paging data
$ List = $ Data-> where ($ map)-> order ('create _ time')-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select ();
$ This-> assign ('list', $ list); // assign a value to a dataset
$ This-> assign ('page', $ show); // value-assigned paging output

$ This-> display (); // output Template

If no data exists, the page is blank. Therefore, before testing, make sure that your data table contains a certain amount of data. Otherwise, the paging effect may not be visible. If you use the page method to query data, you can change it:

$ Data = M ('data'); // instantiate a Data object
Import ('org. Util. page'); // import the paging class
$ Count = $ Data-> where ($ map)-> count (); // query the total number of records meeting the requirements
$ Page = new Page ($ count); // The total number of incoming records in the instantiated paging class
// Perform paging Data Query. Note that the first part of the page method parameter is the current page number obtained using $ _ GET [p ].
$ NowPage = isset ($ _ GET ['P'])? $ _ GET ['P']: 1;
$ List = $ Data-> where ($ map)-> order ('create _ time')-> page ($ nowPage. ','. $ Page-> listRows)-> select ();
$ Show = $ Page-> show (); // display the output by Page
$ This-> assign ('page', $ show); // value-assigned paging output
$ This-> assign ('list', $ list); // assign a value to a dataset
$ This-> display (); // output Template


Then, we can add the paging output variable to the template:










[{$ Vo. create_time | date = 'Y-m-d H: I: S', ##}] {$ vo. title}

{$ Page}

Complete (you can also obtain the complete data first and then...

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.