ThinkPHP data paging Page. class. php
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 the Data Object import ('org. util. page '); // import the paging class $ count = $ Data-> where ($ map)-> count (); // query the total number of records meeting the requirements $ map indicates the query condition $ Page = new Page ($ count ); // instantiate the total number of incoming records in the paging class $ show = $ Page-> show (); // display 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 dataset $ this-> assign ('page ', $ show); // value-assigned paging output $ this-> display (); // output Template
Copy code
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 for query, you can change it
$ Data = M ('data'); // instantiate the 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 ); // instantiate the total number of records passed in by the paging class // query the paging data. Note that the first part of the parameters of the page method is the current page number. use $ _ GET [p] to GET $ nowPage = isset ($ _ GET ['P'])? $ _ GET ['P']: 1; $ list = $ Data-> where ($ map)-> order ('create _ Time')-> page ($ nowPage. ','. $ Page-> listRows)-> select (); $ show = $ Page-> show (); // display output by page $ this-> assign ('page ', $ show); // value-assigned paging output $ this-> assign ('LIST', $ list); // value-assigned dataset $ this-> display (); // output Template
Copy code
Then, add the paging output variable to the template.
[ {$vo.create_time|date='Y-m-d H:i:s',###} ] {$vo.title} |
{$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. We can configure the VAR_PAGE configuration parameter to change it:
- 'Var _ page' => 'page:
- Http: // serverName/index. php/Data/index/page/1
- Http: // serverName/index. php/Data/index/page/1 copy the code to set the number of records per page. By default, 20 Data entries are displayed on each page, if you want to change the volume of data displayed on each page, you can pass the second parameter when instantiating the paging class:
- $ Page = new Page ($ count, 5 ); // instantiate the total number of records passed in by the paging class and 5 records are displayed on each Page. Copy the code. Because the $ Page-> listRows attribute is used in the query method, you do not need to change it, however, if you use numbers directly 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); copy the code because the U function is called internally, the final paging jump link generated by the paging class will automatically generate an address consistent with the current URL mode based on the current URL settings, so you do not need to worry about the URL address affected by the paging link parameters. 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 route 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 display 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 ('head', 'member '); the attributes supported by the copy code setConfig method include:
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.