ThinkPHP3.1 quick start (18) data paging

Source: Internet
Author: User
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:
  1. Create table if not exists 'think _ data '(
  2. 'Id' smallint (4) unsigned not null AUTO_INCREMENT,
  3. 'Title' varchar (255) not null,
  4. 'Content' varchar (255) not null,
  5. 'Create _ time' int (11) unsigned not null,
  6. Primary key ('id ')
  7. ) 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:
  1. $ Data = M ('data'); // instantiate a Data object
  2. Import ('org. Util. page'); // import the paging class
  3. $ Count = $ Data-> where ($ map)-> count (); // The total number of records that meet the query requirements. $ map indicates the query conditions.
  4. $ Page = new Page ($ count); // The total number of incoming records in the instantiated paging class
  5. $ Show = $ Page-> show (); // display the output by Page
  6. // Query paging data
  7. $ List = $ Data-> where ($ map)-> order ('create _ time')-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select ();
  8. $ This-> assign ('list', $ list); // assign a value to a dataset
  9. $ This-> assign ('page', $ show); // value-assigned paging output
  10. $ 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:
  1. $ Data = M ('data'); // instantiate a Data object
  2. Import ('org. Util. page'); // import the paging class
  3. $ Count = $ Data-> where ($ map)-> count (); // query the total number of records meeting the requirements
  4. $ Page = new Page ($ count); // The total number of incoming records in the instantiated paging class
  5. // Perform paging Data Query. Note that the first part of the page method parameter is the current page number obtained using $ _ GET [p ].
  6. $ NowPage = isset ($ _ GET ['P'])? $ _ GET ['P']: 1;
  7. $ List = $ Data-> where ($ map)-> order ('create _ time')-> page ($ nowPage. ','. $ Page-> listRows)-> select ();
  8. $ Show = $ Page-> show (); // display the output by Page
  9. $ This-> assign ('page', $ show); // value-assigned paging output
  10. $ This-> assign ('list', $ list); // assign a value to a dataset
  11. $ This-> display (); // output Template
Copy the code and add the paging output variable to the template:
  1. [{$ Vo. create_time | date = 'Y-m-d H: I: S', ##}] {$ vo. title}
  2. 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:
  1. Http: // serverName/index. php/Data/index/p/1
  2. Http: // serverName/index. php/Data/index/p/2
Copy the code and we can configure the VAR_PAGE configuration parameter to change it:
  1. 'Var _ page' => 'Page'
Copy the code to convert the paging address:
  1. Http: // serverName/index. php/Data/index/page/1
  2. 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:
  1. $ 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:
  1. Foreach ($ map as $ key => $ val ){
  2. $ Page-> parameter. = "$ key =". urlencode ($ val ).'&';
  3. }
Copy the code or directly input an array:
  1. $ 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:
  1. Http: // serverName/data/index/1
  2. Http: // serverName/data/index/2
  3. Http: // serverName/data/index/3
Copy the URL routing address such as the code, then we can set
  1. $ 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:
  1. $ 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:
  1. $ 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.

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.