thinkphp implementing an array pagination example _php tutorial

Source: Internet
Author: User
paging in the framework of thinkphp。 The company's website is based on the thinkphp framework, has been heard thinkphp name, finally have the opportunity to combat. Thinkphp is an MVC architecture, and MVC is no stranger to any iters, model-View-controller. He separates the logic and the data, with a lot less tedious process. In fact, in the official information has been described in detail how to paging, Portal: Http://document.thinkphp.cn/manual_3_2.html#data_page

However, it does not apply to the data has been removed from the DB, and converted to an array of cases, I contacted PHP uttered 2 months, contact thinkphp 3 weeks. Before spending a lot of time on official documents, to be familiar with thinkphp. It's ax. The official documentation is described here as a comparison:

(Only the first method on the document): using the Page class and the Limit method, the code is as follows:

Copy the Code code as follows:
$User = M (' User '); Instantiating a User object

$count = $User->where (' Status=1 ')->count ();//query satisfies the required total number of records

$Page = new \think\page ($count, 25);//Instantiate the total number of incoming records for the paging class and the number of records displayed per page (25)

$show = $Page->show ();//pagination display output

Querying for paging data note the parameters of the limit method to use the properties of the page class

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

The basic idea is to calculate the total number of records first, then page by the number of records displayed per page set. The page class that is packaged with thinkphp can be easily implemented. The implementation focuses on line 11th, where the limit method is to extract data from the query data in accordance with certain rules. But what about the data being taken out?

We used PHP's own function array_slice (). Definition here: http://www.php.net/manual/en/function.array-slice.php

is actually the array version of the Limit method. Well, the tools have been found, and the implementation is easy. Directly on the code:

Copy the Code code 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 truncated, preserving only the implementation details.

The 3rd line Getnodelists method extracts the data from the database and assigns it to the array nodelist.

Line 5th Count counts the number of elements in the array.

6th behavior the page class passed in parameters.

The Array_slice function in line 7th replaces the limit method. Same principle.

The 9th line assigns a value to the template using the Assign method. Definition here: http://document.thinkphp.cn/manual_3_2.html#assign

The 10th line is the same.

The following is the code in view:

Copy the Code code as follows:

{$page}



If this is the case, the effect shown is not friendly. Then find out the definition of the page class:

Copy the Code code as follows:
Pagination Display Customization

Private $config = Array (

' Header ' = ' ' total%total_row% records ',

' Prev ' = ' prev ',

' Next ' = ' next page ',

' First ' = ' Page One ',

' Last ' = ' ...%total_page% ',

' Theme ' = '%first%%up_page%%link_page%%down_page%%end% ',

Add a space before and after the page. Now you can see the effect:

With the big background or a good match, of course, you can set different results according to their own situation.

http://www.bkjia.com/PHPjc/754033.html www.bkjia.com true http://www.bkjia.com/PHPjc/754033.html techarticle paging is implemented under the framework of thinkphp. The company's website is based on the thinkphp framework, has been heard thinkphp name, finally have the opportunity to combat. Thinkphp is an MVC architecture, MVC for any ite ...

  • 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.