ThinkPHP3.1 data paging query implementation method

Source: Internet
Author: User
Tags urlencode

We first create a think_data table in the database for testing:

The code is as follows: Copy code
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:

The code is as follows: Copy code

$ 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


As you can see, only the {$ page} variable needs to be output in the template.


Paging settings
Set paging variables
By default, the variable for paging value passing is p, and the generated paging jump address may be similar:

The code is as follows: Copy code

Http: // serverName/index. php/Data/index/p/1
Http: // serverName/index. php/Data/index/p/2

We can configure the VAR_PAGE configuration parameters to change:

The code is as follows: Copy code

'Var _ page' => 'Page'

The paging address is changed:

The code is as follows: Copy code

Http: // serverName/index. php/Data/index/page/1
Http: // serverName/index. php/Data/index/page/1

Set the number of records per page
By default, 20 data entries are displayed on each page. If you want to change the data volume displayed on each page, you can pass the second parameter when instantiating the paging class:
1. $ Page = new Page ($ count, 5); // instantiate the total number of incoming records of the paging class and 5 records are displayed on each Page
Because the $ Page-> listRows attribute is used in the query method, you do not need to change it. However, if you directly use numbers in the query method, remember to change them together.
The following is the display result of the official paging example:

Incoming paging condition
By default, the paging class automatically obtains the POST (priority) or GET variable of the current page as the pass value of the page jump. If you need to specify the parameters for the current page jump, you can set the parameter attribute. The parameter attribute supports two methods for passing values: string and array. String

The code is as follows: Copy code

Var1 = val1 & var2 = val2. .. format, for example:
Foreach ($ map as $ key => $ val ){
$ Page-> parameter. = "$ key =". urlencode ($ val ).'&';
}

Or directly input the array:

$ Page-> parameter = array_map ('urlencode', $ map );

Because the U function is called internally, the paging jump link generated by the paging class will automatically generate an address consistent with the current URL mode according to the current URL settings, therefore, you do not need to worry that the parameters of the paging link affect the URL address.
Paging route support
If your paging link address uses a route, you can set url parameters. For example, assume that the format of our paging URL address is:

The code is as follows: Copy code

Http: // serverName/data/index/1
Http: // serverName/data/index/2
Http: // serverName/data/index/3

You can set this URL route address.

The code is as follows: Copy code

$ Page-> url = 'data/index ';

After the URL is set, the URL format of the pagination class is automatically generated.
Note: If the url parameter and parameter are both used, the latter is invalid.
Set the number of displayed pages
You can set relevant attributes after instantiating the paging class. By default, the page displays 5 pages, which can be modified as follows:

The code is as follows: Copy code

$ Page-> rollPage = 3;

In this way, only three tabs can be seen at the same time on the page.


Page display customization
The preceding section describes how to set the page display effect (including the style. The default paging effect may not meet all requirements. The paging class provides a setConfig method to modify some default settings.
For example:

The code is as follows: Copy code
$ Page-> setConfig ('header', 'Member ');

The setConfig method supports the following attributes:

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.