Php self-written nice paging class

Source: Internet
Author: User
A nice paging class written by php itself is not complete. it is not verified in some cases, but can be used. for the paging effect, see the bottom of the article. Except for the code that links to the database, all the other codes are pasted. For learning and use ~

1
 PageSize = $ pageSize; 19 $ this-> showPage = $ showPage; 20 $ this-> count = $ count; 21 $ this-> page = $ this-> checkPage ($ currentPage); 22 $ this-> mergyStr = ''; 23 $ this-> skip = $ skip; 24} 25 26 // check the validity of the passed $ page 27 public function checkPage ($ currentPage) {28 if ($ currentPage <1 | empty ($ currentPage )) {29 $ currentPage = 1; 30} 31 if ($ currentPage> $ this-> totalPages () {32 $ currentPage = $ this-> totalP Ages (); 33} 34 return $ currentPage; 35} 36 37 // calculates the offset 38 public function pageOffset () {39 return ($ this-> showPage-1)/2; 40} 41 42 // calculate the total number of pages 43 public function totalPages () {44 return ceil ($ this-> count/$ this-> pageSize ); 45} 46 47 // get the page URL 48 public function getPageUrl () {49 $ CurrentUrl = $ _ SERVER ["REQUEST_URI"]; 50 $ arrUrl = parse_url ($ CurrentUrl ); 51 $ urlQuery = $ arrUrl ["query"]; 52 53 if ($ u RlQuery) {54 // print_r ($ this-> page); 55 $ urlQuery = preg_replace ("/(^ | &) page = /". $ this-> page, "", $ urlQuery); 56 $ CurrentUrl = str_replace ($ arrUrl ["query"], $ urlQuery, $ CurrentUrl ); 57 58 if ($ urlQuery) {59 $ CurrentUrl. = "& page"; 60} 61 else $ CurrentUrl. = "page"; 62 63} else {64 $ CurrentUrl. = "? Page "; 65} 66 67 return $ CurrentUrl; 68} 69 70 // The page number shows the 71 public function GetPagerContent () {72 $ start = 1; 73 $ end = $ this-> totalPages (); 74 $ this-> mergyStr. ="

"; 75 if ($ this-> page> 1) {76 $ this-> mergyStr. = "getPageUrl (). "= ". "1 '> homepage"; 77 $ this-> mergyStr. = "getPageUrl (). "= ". ($ this-> page-1 ). "'> Previous Page"; 78} else {79 $ this-> mergyStr. = "homepage"; 80 $ this-> mergyStr. = "previous page"; 81} 82 83 if ($ this-> totalPages ()> $ this-> showPage) {84 if ($ this-> page> $ this-> pageoffset () + 1) {85 $ this-> mergyStr. = "... "; 86} 87 88 if ($ this-> page> $ this-> pageoffset () {89 $ star T = $ this-> page-$ this-> pageoffset (); 90 $ end = $ this-> totalPages ()> $ this-> page + $ this-> pageoffset ()? $ This-> page + $ this-> pageoffset (): $ this-> totalPages (); 91} 92 else {93 $ start = 1; 94 $ end = $ this-> totalPages ()> $ this-> showPage? $ This-> showPage: $ this-> totalPages (); 95} 96 97 if ($ this-> page + $ this-> pageoffset ()> $ this-> totalPages () {98 $ start = $ start-($ this-> page + $ this-> pageoffset ()-$ end ); 99} 100 101} 102 103 for ($ I = $ start; $ I <= $ end; $ I ++) {104 if ($ I ==$ this-> page) {105 $ this-> mergyStr. = "{$ I}"; 106} else {107 $ this-> mergyStr. = "getPageUrl (). "= ". $ I. "'> {$ I}"; 108} 109 110} 111 if ($ this-> totalPages ()> $ this-> showPage & $ this-> totalPages ()> $ this-> page + $ this-> pageoffset () {113 $ this-> mergyStr. = "... "; 114} 115 116 if ($ this-> page <$ this-> totalPages () {117 $ this-> mergyStr. = "getPageUrl (). "= ". ($ this-> page + 1 ). "'> next page"; 118 $ this-> mergyStr. = "getPageUrl (). "= ". $ this-> totalPages (). "'> last page"; 119} else {120 $ this-> mergyStr. = "next page"; 121 $ this-> mergyStr. = "Last page"; 122} 123 $ this-> mergyStr. = "total {$ this-> totalPages ()} page"; 125 126 // Display jump box 127 if ($ this-> skip = true) {128 $ this-> mergyStr. = $ this-> skipNumPage (); 129} 130 131 $ this-> mergyStr. ="

"; 132 133 print_r ($ this-> mergyStr); 134} 135 136 // jump page 137 public function skipNumPage () {138 $ this-> mergyStr. = ""; 142} 143 144}

The above is all the code of the class file, and the following is the call instance:

 "; Echo"Follow_trade_idFollow_id"; While ($ rs = mysql_fetch_assoc ($ query) {echo"{$ Rs ['Follow _ trade_id ']}{$ Rs ['Follow _ id']}";} Echo""; // Read the pagination bar $ p-> GetPagerContent ();

Style CSS:

 

Effect:

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.