thinkphp Data Paging Page.class.php

Source: Internet
Author: User

Get Pagination Class

Thinkphp provides an Extended class library page for data paging, which can be downloaded from http://www.thinkphp.cn/extend/241.html, or download the official Full expansion pack (http://www.thinkphp.cn/down/ 253.html) also contains the paging extension class. Put the extracted Page.class.php into the thinkphp/extend/library/org/util/(if not manually created) below the directory.
Of course, the location of the Extension class library is quite casual, you can also put in the project's class library directory, the difference is only the path you import different.

Paging Query

The paging class needs to be combined with the query, and we can use the thinkphp limit method or the page method to get the current paging data (there is also a way to get the full data and then the front page pagination, which is not included in this article or recommended). Using the Limit method or the page method is independent of the database type.

We first create a think_data 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) is not NULL,      ' create_time ' int (one) unsigned not null,      PRIMARY KEY (' id ')    ) Engine=myisam  DEFAULT Charset=utf8;


Copy Code

To use a paged query, you generally need to make two queries, that is, the first query gets the total amount of data that meets the criteria, and then the second time you query the current paging data, the effect is to tell the paging class the current total number of data in order to calculate the total pages generated (if your display only needs to page up and down, In fact, the total number of queries can be omitted or cached).

A standard example of paging usage is as follows:

    $Data = M (' Data '); Instantiate the Data Object    import (' ORG. Util.page '),//Import the paging class    $count      = $Data->where ($map)->count ();//The total number of records that the query satisfies requirements $map represents the query condition    $Page       = New page ($count);//Instantiate the total number of incoming records    in the paging class $show       = $Page->show ();//pagination display output    //Paging data query    $list = $Data where ($map)->order (' Create_time ')->limit ($Page->firstrow. ', '. $Page->listrows)->select ();    $this->assign (' list ', $list);//Assignment Data set    $this->assign (' page ', $show);//Assignment paging output    $this->display (); /Output Template


Copy Code

If there is no data, pagination appears blank. So before you test, make sure you have some data in your data table, or you may not see the effect of paging. If you query using the page method, you can change to

    $Data = M (' Data '); Instantiate the Data Object    import (' ORG. Util.page '),//Import paging class    $count      = $Data->where ($map)->count ();//query satisfies the required total number of records    $Page       = new Page ($ count);//Instantiate paging class incoming total records    //Paging data query Note the previous part of the parameter of the page method is the current number of pages using $_get[p] get    $nowPage = isset ($_get[' P '])? $_get[' P ']:1;    $list = $Data->where ($map)->order (' Create_time ')->page ($nowPage. ', '. $Page->listrows)->select ();    $show       = $Page->show ();//pagination display output    $this->assign (' page ', $show);//Assignment paging output    $this->assign (' List ', $list);//Assignment Data set    $this->display ();//Output template



Copy Code

Then we add the paging output variable to the template


 
   
 
  
 
  
  
{$page}



Copy Code

You can see that the paging output only needs to be output in the template with {$page} variables.

Paging settings

Set Paging variables

By default, the paging-value variable is p, and the resulting paging-jump address may resemble the following:
    1. Http://serverName/index.php/Data/index/p/1
    2. Http://serverName/index.php/Data/index/p/2

Copy Code

We can configure the Var_page configuration parameters to change:
    1. ' Var_page ' = ' PAGE '

Copy Code

The paging address becomes:
    1. Http://serverName/index.php/Data/index/page/1
    2. Http://serverName/index.php/Data/index/page/1

Copy Code

Set the number of records per page

By default, pagination displays 20 data per page, and if you want to change the amount of data displayed per page, you can then instantiate the second parameter when you are instantiating the paging class:
    1. $Page = new Page ($count, 5);//Instantiate the total number of incoming records in the paging class and show 5 records per page

Copy Code

Since we used the $page->listrows property in the Query method, there is no need to change it, but if you are using numbers directly in the query method, remember to change them together.
Here's how the official pagination example looks:

Incoming paging condition

By default, the paging class automatically gets the current page's post (priority) or get variable as the value of the paging jump, and if you need to specify parameters to pass in the current paging jump, you can set the parameter property by setting the parameter property to support 2 ways of passing the value: string and array. String using Var1=val1&var2=val2 ... Format, for example:
    1. foreach ($map as $key = = $val) {
    2. $Page->parameter. = "$key =". UrlEncode ($val). ' & ';
    3. }

Copy Code

Or simply pass in the array:
    1. $Page->parameter = Array_map (' UrlEncode ', $map);

Copy Code

Due to the internal invocation of the U function, the paging class will eventually generate a paging jump link automatically generated according to the current URL settings and the current URL pattern consistent address, so there is no need to worry about the paging link parameters affect the URL address.

Paging Routing Support

If your paging link address is routed, then you can set the URL parameters, for example, suppose our paging URL address format is:
    1. Http://serverName/data/index/1
    2. Http://serverName/data/index/2
    3. Http://serverName/data/index/3

Copy Code

Such URL routing address, then we can set the
    1. $Page->url = ' data/index ';

Copy Code

When set, the link address of the paging class automatically generates the URL format address above.
Note that the URL parameter and the parameter are used together, and the latter is invalid.

Set the number of pages to display

You can set the related properties after instantiating the paging class. By default, the page is displayed with 5 pages, which we can modify:
    1. $Page->rollpage = 3;

Copy Code

This way, only 3 pages can be seen on the page at a time

Pagination Display Customization

The above is the paging parameter setting, below how to set the page display effect (including style). The default paging effect may not meet all requirements, and the paging class provides a Setconfig method to modify some of the default settings. For example:
    1. $page->setconfig (' header ', ' a member ');

Copy Code

The properties supported by the Setconfig method include:
Header Header description information, default value "bar Record"
Prev Previous page description information, default value is "Previous page"
Next Next page describes the information, the default value is "Next page"
First The first page describes the information, the default value is "first page"
Last The last page describes the information, the default value is "last Page"
Theme The pagination topic describes the information, including the combination of all the above elements, setting this property can change the pagination of the individual units display location, the default value is
"%totalrow%%header%%nowpage%/%totalpage% page%uppage%%downpage%%first%%prepage%%linkpage%%nextPage%%end%"
Setting the above properties through Setconfig is a perfect way to customize your page 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.