Php paging code

Source: Internet
Author: User
Php paging code/* idea 1. get the URL in the address bar 2. analyze the query part in the URL-that is? Part 3. query is analyzed as an array. 4. + 1,-1 in the array to form two new arrays 5. concatenate the new array into the query part and combine the previous page and Next Page connection address * // paging class php paging class code
/* Idea 1. get the URL in the address bar 2. analyze the query section in the URL-that is? Part 3. query is analyzed as an array. 4. + 1,-1 in the array to form two new arrays 5. concatenate the new array into the query part and combine the previous Page and Next Page connection address * // The number of all lines in the paging class Page {public $ total, retrieve public $ prePage = 10 from the database; // number of entries per page protected $ curr = 1; // Default current page number public function _ construct ($ total, $ prePage = '') {$ this-> total = $ total; // place the total entry information in the total attribute if ($ prePage> 0) {$ this-> prePage = $ prePage; // place the number of pages per page in the perPage attribute} // calculate the current page number if (isset ($ _ GET ['Page']) & ($ _ GET ['Page'] + 0)> 0) {$ th Is-> curr =$ _ GET ['Page'] + 0 ;}// the main function public function showPage () {if ($ this-> total <= 0) {return ''; // if the total entry is <= 0, an empty string is returned.} $ cnt = ceil ($ this-> total/$ this-> prePage ); // calculate the total number of pages and enter an integer. // based on the current page, calculate the previous page and next page/* Analyze the url. What are the situations? Xx. phpxx. php? Id = 5xx. php? Page = 3xx. php? Id = 5 & page = 3 * // The final generated URL must contain page = N $ url = $ _ SERVER ['request _ URI ']; $ parse = parse_url ($ url); // put the URL analysis result in the array // print_r ($ parse); // ensure that the parameter contains pageif (! Isset ($ parse ['query']) {$ parse ['query'] = 'page = '. $ this-> curr;} // analyze the query string into an array and make sure that the page option parse_str ($ parse ['query'], $ parms) is available again; if (! Array_key_exists ('page', $ parms) {$ parms ['Page'] = $ this-> curr;} // test the preceding four conditions, the page parameter can generate // print_r ($ parms); // you can determine whether there are other parameters besides page (if (count ($ parms) = 1) {$ url = $ parse ['path']. '? ';} Else {unset ($ parms ['Page']); $ url = $ parse ['path'].'? '. Http_build_query ($ parms ). '&';} // echo $ url $ prev = $ this-> curr-1; $ next = $ this-> curr + 1; // homepage $ indexLink = 'homepage'; // Previous Page if ($ prev <1) {$ prevLink = '';} else {$ prevLink = 'previous page ';} // next Page if ($ next> $ cnt) {$ nextLink = '';} else {$ nextLink = 'next page ';} // end page $ lastLink = 'end page'; // echo $ indexLink. ''. $ prevLink. ''. $ nextLink. ''. $ lastLink; // previous page, 1 2 3 4 5 Next Page $ start = $ this-> curr-(5-1)/2; // calculate the start page number on the left $ end = $ this-> curr + (5-1)/2; // calculate the start page number on the right. // if the start page on the left is displayed, if the value is less than 1, add the less than 1 part to the right if ($ start <1) {$ end + = (1-$ start); $ start = 1; // modify start = 1if ($ end> $ cnt) {$ end = $ cnt ;}/// add the excess part on the right to the left if ($ end> $ cnt) {$ start-= ($ end-$ cnt); $ end = $ cnt; if ($ start <1) {$ start = 1 ;}} // Number of outgoing page number in a loop $ pageStr = ''; for ($ I = $ start; $ I <= $ end; $ I ++) {if ($ I ==$ this-> curr) {$ pageStr. = $ I; continue;} $ pageStr. = ''. $ I. '';} return $ indexLink. $ prevLink. $ pageStr. $ nextLink. $ lastLink ;}}$ page = new Page (); echo $ page-> showPage ();

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.