[Php]/* 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. splice the new array into the query part, and combine the previous page and Next Page connection address... syntaxHighligh [php]/* 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 * // 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 ['pa Ge '] + 0)> 0) {$ this-> curr = $ _ GET ['Page'] + 0 ;}// Main function public function showPage () {if ($ this-> total <= 0) {return ''; // if the total number of entries is <= 0, an empty string is returned.} $ cnt = ceil ($ this-> total/$ this-> prePage); // calculates the total number of pages, perform an integer // based on the current page, calculate the previous page, next page/* Analyze the url. What are the situations? Xx. php xx. php? Id = 5 xx. php? Page = 3 xx. 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 page if (! 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 = 1 if ($ end> $ cnt) {$ end = $ cnt ;}/// add the excess part on the right, add to left if ($ end> $ cnt) {$ start-= ($ end-$ cnt); $ end = $ cnt; if ($ start <1) {$ start = 1 ;}/// Number of outgoing page numbers 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 ();
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