Thinkphp array paging example

Source: Internet
Author: User
This article mainly introduces the array paging example of thinkphp implementation. For more information, see

This article mainly introduces the array paging example of thinkphp implementation. For more information, see

Implement paging in the thinkphp framework. Based on the Thinkphp framework, the company's website has been famous for thinkphp for a long time and finally has the opportunity to practice Thinkphp. Thinkphp is based on the MVC Architecture. MVC is no stranger to any ITers, model-view-controller ). It processes the logic and data separately, removing a lot of tedious processes. In fact, in the official documents, I have already described in detail how to paging the Portal:

However, it does not apply to situations where the data has been retrieved from the database and converted to an array. I have been using PHP for 2 months and thinkphp for 3 weeks. I spent a lot of time on official documents to familiarize myself with thinkphp. It is not mistaken for a firewood engineer. The official document is described as a comparison:

(Only the first method in the document): Use the Page class and limit method, the Code is as follows:

The Code is as follows:


$ User = M ('user'); // instantiate the User object

$ Count = $ User-> where ('status = 1')-> count (); // query the total number of records meeting the requirements

$ Page = new \ Think \ Page ($ count, 25); // instantiate the total number of incoming records in the paging class and the number of records displayed on each Page (25)

$ Show = $ Page-> show (); // display the output by Page

// Perform paging data query. Note that the parameters of the limit Method must use the attributes of the Page class.

$ List = $ User-> where ('status = 1')-> 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

The basic idea is to calculate the total number of records first, and then pagination based on the number of records displayed on each page. The Page class encapsulated by Thinkphp can be easily implemented. The implementation focuses on 11th rows. The limit method extracts data from the query data according to certain rules. But what should I do if the data has been taken out?

We used the built-in function array_slice () in php (). Defined here:

It is actually the limit Method of the Number Group version. All right, the tool is found, and the implementation is very easy. Directly run the Code:

The Code is as follows:


Public function nodeslist (){
$ Portal = new PortalApi;
$ Nodelist = $ portal-> getNodeLists ($ this-> uid );

$ Count = count ($ nodelist ['data']);
$ P = new Page ($ count, 10 );
$ Lists = array_slice ($ nodelist ['data'], $ p-> firstRow, $ p-> listRows );
$ Page = $ p-> show ();
$ This-> assign ('page', $ page );
$ This-> assign ('nodes ', $ lists );
$ This-> display ();
}

The code is deleted, and only the implementation details are retained.

The getNodeLists method of Row 3 extracts data from the database and assigns the value to the array nodelist.

The number of elements in the array is calculated based on the count value of the row 5th.

6th pass in parameters for the behavior Page class.

The array_slice function of row 7th replaces the limit method. Same principle.

The assign method is used to assign values to the template in row 9th. Defined here: # assign

The same is true for 10th rows.

The code in view is as follows:

The Code is as follows:


{$ Page}



If this is the case, the effect is unfriendly. Find out the definition of the Page class:

The Code is as follows:


// Custom display by PAGE

Private $ config = array (

'Header' => '% TOTAL_ROW % records in total ',

'Prev' => 'previous page ',

'Next' => 'Next page ',

'First' => 'page 1 ',

'Last' => '... % TOTAL_PAGE % ',

'Theme '=>' % FIRST % UP_PAGE % LINK_PAGE % DOWN_PAGE % END % ',

Add spaces before and after the number of pages. Now you can see the effect:

It works well with the big background. Of course, you can set different effects based on your own situation.

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.