Use ThinkPHP to implement paging, and thinkphp to implement Paging

Source: Internet
Author: User

Use ThinkPHP to implement paging, and thinkphp to implement Paging

The functions described in the previous articles (upload, thumbnail, verification code, and automatic verification form) are implemented based on classes encapsulated by the ThinkPHP framework, so this time I write a paging class for use in the framework.

First, create a Tools folder in the root directory, and create a Page. class. php file under the Tools Folder, so that custom tool classes can be placed in the Tools Folder.

This type of encapsulation includes the following functions: Get the request address, start page, which display, end page, page number list (homepage hyperlink, Previous Page, page number list hyperlink, next page, at the end of the page, jump), enough for paging!

The following is the Page. class. php code.

<? Php // The namespace name is consistent with the parent directory tools // cause: the current Page. class. php files will be automatically loaded. // "tools" will be changed to the parent directory of the file at the same time, and the namespace Tools of the Page file will be obtained; class Page {private $ total; // total number of records in the data table private $ listRows; // The number of lines displayed on each Page is private $ limit; private $ uri; // The URL of the current link is private $ pageNum; // The number of pages is private $ config = array ('header' => "records", "prev" => "Previous Page ", "next" => "next page", "first" => "first page", "last" => "last page"); private $ listNum = 8; // limit the number of page number lists/** $ total number of records * $ list Rows displays the number of Rows per page */public function _ construct ($ total, $ listRows = 10, $ pa = "") {$ this-> total = $ total; // total number of records in the data table $ this-> listRows = $ listRows; // you can specify the number of lines displayed on each page. $ this-> uri = $ this-> getUri ($ pa ); // request address $ this-> page =! Empty ($ _ GET ["page"])? $ _ GET ["page"]: 1; // current page $ this-> pageNum = ceil ($ this-> total/$ this-> listRows ); // total number of pages $ this-> limit = $ this-> setLimit (); // Limit the length of each page} private function setLimit () {return "limit ". ($ this-> page-1) * $ this-> listRows. ", {$ this-> listRows}";} // request address private function getUri ($ pa) {$ url = $ _ SERVER ["REQUEST_URI"]. (strpos ($ _ SERVER ["REQUEST_URI"], '? ')? '':"? "). $ Pa; $ parse = parse_url ($ url); if (isset ($ parse ["query"]) {parse_str ($ parse ['query'], $ params ); unset ($ params ["page"]); $ url = $ parse ['path']. '? '. Http_build_query ($ params);} return $ url;} function _ get ($ args) {if ($ args = "limit") return $ this-> limit; else return null;} // The start page, from which the private function start () {if ($ this-> total = 0) return 0; else return ($ this-> page-1) * $ this-> listRows + 1;} // from which the private function end () {return min ($ this-> page * $ this-> listRows, $ this-> total);} // homepage hyperlink private function first () {$ html = ""; if ($ this-> page = 1) $ Html. = ''; else $ html. = "& nbsp; <a href = '{$ this-> uri} & page = 1' >{$ this-> config ["first"]} </a> & nbsp; "; return $ html;} // prev function prev () {$ html =" "; if ($ this-> page = 1) $ html. = ''; else $ html. = "& nbsp; <a href = '{$ this-> uri} & page = ". ($ this-> page-1 ). "'>{$ this-> config [" prev "]} </a> & nbsp;"; return $ html ;} // page number list hyperlink private function pageList () {$ linkPage = ""; $ inum = floor ($ thi S-> listNum/2); for ($ I = $ inum; $ I >=1; $ I --) {$ page = $ this-> page-$ I; if ($ page <1) continue; $ linkPage. = "& nbsp; <a href = '{$ this-> uri} & page = {$ page}' >{$ page} </a> & nbsp ;";} $ linkPage. = "& nbsp; {$ this-> page} & nbsp;"; for ($ I = 1; $ I <= $ inum; $ I ++) {$ page = $ this-> page + $ I; if ($ page <= $ this-> pageNum) $ linkPage. = "& nbsp; <a href = '{$ this-> uri} & page = {$ page}'> {$ page} </a> & nbsp ;"; else break;} return $ linkPage;} // next page private Function next () {$ html = ""; if ($ this-> page = $ this-> pageNum) $ html. = ''; else $ html. = "& nbsp; <a href = '{$ this-> uri} & page = ". ($ this-> page + 1 ). "'>{$ this-> config [" next "]} </a> & nbsp;"; return $ html ;} // private function last () {$ html = ""; if ($ this-> page = $ this-> pageNum) $ html. = ''; else $ html. = "& nbsp; <a href = '{$ this-> uri} & page = ". ($ this-> pageNum ). "'>{$ this-> config [" last "]} </a> & nbsp; & nbs P; "; return $ html;} // jump to private function goPage () {return '& nbsp; <input type =" text "onkeydown =" javascript: if (event. keyCode = 13) {var page = (this. value> '. $ this-> pageNum. ')? '. $ This-> pageNum. ': this. value; location = \''. $ this-> uri. '& page = \' + page + \ '} "value = "'. $ this-> page. '"> $ this-> pageNum. ')? '. $ This-> pageNum. ': this. previussibling. value; location = \''. $ this-> uri. '& page = \' + page + \ '"> & nbsp;';} // page number list function fpage ($ display = array, 4,5, 6,7, 8) {$ html [0] = "& nbsp; total <B >{$ this-> total} </B >{$ this-> config ["header"]} & nbsp ;"; $ html [1] = "& nbsp; <B> ". ($ this-> end ()-$ this-> start () + 1 ). "</B>, <B> {$ this-> start ()}-{$ this-> end ()} </B> & nbsp; & nbsp; "; $ html [2] =" & nbsp; <B> {$ this-> page}/{$ this-> pageNum} </B> page & nbsp ;"; $ html [3] = $ this-> first (); $ html [4] = $ this-> prev (); $ html [5] = $ this-> pageList (); $ html [6] = $ this-> next (); $ html [7] = $ this-> last (); $ html [8] = $ this-> goPage (); $ fpage = ''; foreach ($ display as $ index) {$ fpage. = $ html [$ index] ;}return $ fpage ;}}

Controller code:

// Product list function showlist () {// implement paging effect $ goods = D ('goods '); // ① total number of records obtained for data $ total = $ goods-> count (); // select count (*) from sw_goods; $ per = 7; // 7 records are displayed on each Page. // ② instantiated paging class $ page_obj = new \ Tools \ Page ($ total, $ per); // ③ custom SQL statement, obtain information on each page $ SQL = "select * from sw_goods order by goods_id desc ". $ page_obj-> limit; $ info = $ goods-> query ($ SQL); // ④ get the page number list $ pagelist = $ page_obj-> fpage (array (3, 4, 5, 6, 7, 8); // allocate $ this-> assign ('pagelist', $ pagelist); $ this-> assign ('info', $ info ); $ this-> display ();}

Front-end code

<{$pagelist}>

  

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.