ThinkPHP data paging Page. class. php_PHP tutorial

Source: Internet
Author: User
ThinkPHP data paging Page. class. php. Download the ThinkPHP data Page. class. php
The Page class ThinkPHP provides a data paging extension class library Page, which can be downloaded at http://www.thinkphp1.cn/extend/241.html. the official extension package (http://www.thinkphp1.cn/down/253.html) has also included the Page 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 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 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


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



Then, add the paging output variable to the template.


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

{$page}




As you can see, 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 we can configure VAR_PAGE configuration parameters to change:
    1. 'Var _ page' => 'Page:
      1. Http: // serverName/index. php/Data/index/page/1
      2. Http: // serverName/index. php/Data/index/page/1 sets the number of records on each page. by default, 20 Data records are displayed on each page. if you want to change the Data volume displayed on each page, when instantiating the paging class, you can pass the second parameter:
        1. $ 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. because the $ Page-> listRows attribute is used in the query method, you do not need to change the attribute, 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:
          1. Foreach ($ map as $ key => $ val ){
          2. $ Page-> parameter. = "$ key =". urlencode ($ val ).'&';
          3. } Or directly input the array:
            1. $ Page-> parameter = array_map ('urlencode', $ map); 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:
              1. Http: // serverName/data/index/1
              2. Http: // serverName/data/index/2
              3. For URL routing addresses such as http: // serverName/data/index/3, you can set
                1. $ 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. 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; in this way, only three pages can be viewed simultaneously 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 ('head', 'Member '); attributes supported by the 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.

                      The retrieve Page class ThinkPHP provides an extended class library Page for data paging. you can download the Page at http://www.thinkphp1.cn/extend/241.html...

Related Article

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.