thinkphp use of experience sharing-pagination class page usage _php tips

Source: Internet
Author: User

The page class in thinkphp is in thinkphp/extend/library/org/util/page.class.php, so you introduce the page class before you use it:

Copy Code code as follows:

Import (' ORG. Util.page '); Introduction of Page Class
$db = M (' abc ');//materialized datasheet ABC
$where = Array (
' ID ' => ' 2 ';
//Conditional statement $where, the value of the field ID in the example table is 2
$count = $db->where ($where)->count ()//Get the total number of eligible data count
$page = new Page ($count, 10);//Instantiate page class, total incoming data and 10 items per page
$limit = $page->firstrow. ',' . $page->listrows;//the number of data and content per page $limit
$result = $db->where ($where))->limit ($limit)->select ()//Paging query results
$this->result = $result;//Assignment
$this->show = $page->show ()//Get the bottom information of pagination

The above code is the basic statement of the Page class implementation, of course, friends like to use native SQL statements can also cooperate with native SQL statements to implement query paging:

Copy Code code as follows:

Import (' ORG. Util.page '); Introduction of Page Class
$db = M (' abc ');//materialized datasheet ABC
$where = Array (
' ID ' => ' 2 ';
//Conditional statement $where, the value of the field ID in the example table is 2
$count = $db->where ($where)->count ()//Get the total number of eligible data count
$page = new Page ($count, 10);//Instantiate page class, total incoming data and 10 items per page
$Modle = new Model ();
$sql = ' Select Id,name from ABC where '. $where. ' Limit '. $page->firstrow. $page->listrows;//sql Statement
$result = $Modle->query ($sql);//Execute SQL statement
$this->result = $result
$this->show= $page->show ();

Of course, the contents of a distributed query can also be processed and then assigned to the data after the query, such as

Copy Code code as follows:

...

$result = $db->where ($where))->limit ($limit)->select ()//Paging query results
$res = ABC ($result);//abc method (custom method or PHP function) to sort or reorganize the result $result
$this->result = $res;//Assignment

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.