We have learned how to query data. Today, we will learn how to paging and display data. We have learned how to query data. Today, we will learn how to paging and display data.
The retrieve paging class ThinkPHP provides a data paging extension class library Page that can be downloaded at http://www.thinkphp.cn/extend/241.html. the official extension package (http://www.thinkphp.cn/down/253.html) has also included the paging extension class. Put the decompressed Page. class. php in the ThinkPHP/Extend/Library/ORG/Util/Directory (if not, manually create it.
Of course, the location of the extension class library is quite random. you can also put it under the project class library directory. The difference is that the import path is different.
Paging queries must be combined with queries. 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_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 copy the code, you need to query the code by page. Generally, you need to query the code twice, that is, the first query gets 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 the copied code does not have any data, 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 for query, 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
Copy the code and add the paging output variable to the template:
-
-
- [{$ Vo. create_time | date = 'Y-m-d H: I: S', ##}] {$ vo. title}
- Class = "result page"> {$ page}
Copy the code and you can see that only the {$ page} variable needs to be output in the template.
By default, pagination variables are set to p. The generated paging jump address may be similar to the following:
- Http: // serverName/index. php/Data/index/p/1
- Http: // serverName/index. php/Data/index/p/2
Copy the code and we can configure the VAR_PAGE configuration parameter to change it:
- 'Var _ page' => 'Page'
Copy the code to convert the paging address:
- Http: // serverName/index. php/Data/index/page/1
- Http: // serverName/index. php/Data/index/page/1
Copy the code to set the default number of records per page. if you want to change the data volume displayed per page, when instantiating the paging class, you can pass the second parameter:
- $ Page = new Page ($ count, 5); // The total number of incoming records of the instantiated Page 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:
By default, the paging class will automatically obtain the POST (priority) or GET variable of the current page as the paging jump value. if you need to specify the parameters for the current paging jump, you can set the parameter attribute. the parameter attribute supports two methods for passing values: string and array. The string is in the format of var1 = val1 & var2 = val2. .., for example:
- Foreach ($ map as $ key => $ val ){
- $ Page-> parameter. = "$ key =". urlencode ($ val ).'&';
- }
Copy the code or directly input an 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 setting, therefore, you do not need to worry that the parameters of the paging link affect the URL address. Paging routing supports if your paging jump link address uses a route, you can set url parameters. for example, assume that the format of our paging URL address is:
- Http: // serverName/data/index/1
- Http: // serverName/data/index/2
- Http: // serverName/data/index/3
Copy the URL routing address such as the code, then we can set
- $ Page-> url = 'data/index ';
After the code is copied, the link address of the paging class will automatically generate the URL format address above.
Note: If the url parameter and parameter are both used, the latter is invalid.You can set the page number after instantiating the page type. By default, the page displays 5 pages, which can be modified as follows:
- $ Page-> rollPage = 3;
Copy the code to view only three pages at the same time on the page.
Page display customization the preceding section describes how to set the page display effect (including style. The default paging effect may not meet all requirements. The paging class provides a setConfig method to modify some default settings. For example:
- $ Page-> setConfig ('header', 'Member ');
The setConfig method supports the following attributes:
Header |
Header description. the default value is "record" |
Prev |
Description of the previous page. the default value is "previous page" |
Next |
Next Page Description. the default value is "next page" |
First |
Description on the first page. the default value is "first page" |
Last |
Description on the last page. the default value is "last page" |
Theme |
The description of the paging topic, including the combination of all the above elements. setting this attribute can change the display position of each unit on the page. the default value is "% TotalRow % header % nowPage %/% totalPage % Page % upPage % downPage % first % prePage % linkPage % nextPage % end %" |
You can use setConfig to set the preceding attributes to customize your paging display style.