PHP paging class (customizable style)
GetPages (); // generate a page number array (the key is the page number and the value is the link) * echo $ p-> showPages (1 ); // generate a page number style (you can add a custom style) ** @ author: Dzer * @ version: 09:09:42 * @ Last Modified time: 17:37:13 * // * train of thought: give me a total number of pages, the number of pages to be displayed, the number of items displayed on the current page, and write a method to generate a one-dimensional array, the key-to-page value is the connection. write a method to return a page number with a generated style (and you can add a style as needed). a total of 45 default styles are displayed, with 10 entries per page, current page 1/4 [Homepage] [Previous Page] [1] [2] [3] .. [Next Page] [last Page] */class Page {protected $ count; // total number of entries protected $ showPages; // Number of pages to be displayed protec Ted $ countPages; // total number of pages protected $ currPage; // Current page protected $ subPages; // Number of Entries displayed on each page protected $ href; // connect protected $ page_arr = array (); // Save the generated page number. the page number value is the connection/*** _ construct constructor (obtain the parameters required for paging) * @ param int $ count total number of entries * @ param int $ showPages display pages * @ param int $ current number of currpages * @ param int $ number of subPages displayed per page * @ param string $ href connection (get the current URL if not set) */public function _ construct ($ count, $ showPages, $ currPage, $ subPages, $ href = '') {$ This-> count = $ count; $ this-> showPages = $ showPages; $ this-> currPage = $ currPage; $ this-> subPages = $ subPages; // if the link is not set, obtain the current connection if (empty ($ href) {$ this-> href = htmlentities ($ _ SERVER ['php _ SELF ']);} else {$ this-> href = $ href;} $ this-> construct_Pages ();} /*** getPages returns the page number array * @ return array the one-dimensional array key is the page number value link */public function getPages () {return $ this-> page_arr ;} /*** return the generated page number for showPages * @ param int $ style * @ return st Page number generated by ring */public function showPages ($ style = 1) {$ func = 'pagestyle '. $ style; return $ this-> $ func ();}/*** pageStyle1 paging style (you can refer to this to add a custom style such as pageStyle2 ()) * There are 45 styles in total, with 10 entries displayed on each page. Currently, Page 1/4 [Homepage] [Previous Page] [1] [2] [3] .. [Next Page] [last Page] * @ return string */protected function pageStyle1 () {/* constructs a total of 4523 records on pages in normal mode, with 10 records displayed on each page, current page 1/453 [Homepage] [Previous Page] [1] [2] [3] .. [Next Page] [last Page] */$ pageStr = 'col '. $ this-> count. 'records, displayed on each page '. $ this-> subPages. 'barri'; $ PageStr. = 'current name '. $ this-> currPage. '/'. $ this-> countPages. 'Page'; $ _ GET ['Page'] = 1; $ pageStr. = '[href. '? '. Http_build_query ($ _ GET).' "_ href =" '. $ this-> href .'? '. Http_build_query ($ _ GET ). '"> homepage]'; // if the current page is not the first page, if ($ this-> currPage> 1) {$ _ GET ['Page'] =$ this-> currPage-1; $ pageStr. = '[href. '? '. Http_build_query ($ _ GET).' "_ href =" '. $ this-> href .'? '. Http_build_query ($ _ GET ). '"> Previous page]';} foreach ($ this-> page_arr as $ k => $ v) {$ _ GET ['Page'] = $ k; $ pageStr. = '['. $ k. ']';} // if the current page is smaller than the total number of pages, the next page is displayed if ($ this-> currPagecountPages) {$ _ GET ['Page'] = $ this-> currPage + 1; $ pageStr. = '[href. '? '. Http_build_query ($ _ GET).' "_ href =" '. $ this-> href .'? '. Http_build_query ($ _ GET ). '"> Next page]';} $ _ GET ['Page'] = $ this-> countPages; $ pageStr. = '[href. '? '. Http_build_query ($ _ GET).' "_ href =" '. $ this-> href .'? '. Http_build_query ($ _ GET ). '"> Last Page]'; return $ pageStr;}/*** construct_Pages generate page number array * The key is the page number, the value is link * $ this-> page_arr = Array (* [1] => index. php? Page = 1 * [2] => index. php? Page = 2 * [3] => index. php? Page = 3 *......) */protected function construct_Pages () {// calculate the total number of pages $ this-> countPages = ceil ($ this-> count/$ this-> subPages ); // calculate the front and back pages based on the current page $ leftPage_num = floor ($ this-> showPages/2); $ rightPage_num = $ this-> showPages-$ leftPage_num; // the number displayed on the left is the number displayed on the current page minus the number displayed on the left. for example, the total number displayed on the 7 page is 5. the minimum value on the left is 5-3. the value on the right is 5 + 3 $ left = $ this-> currPage-$ leftPage_num; $ left = max ($ left, 1); // The Minimum left value cannot be less than 1 $ right = $ left + $ this-> showPages-1; // The number of pages displayed on the left minus 1 is the number displayed on the right $ right = min ($ right, $ th Is-> countPages); // The maximum number of pages on the right cannot be greater than the total number of pages $ left = max ($ right-$ this-> showPages +); // determine the right and calculate the left, you must calculate for ($ I = $ left; $ I page_arr [$ I] = $ this-> href. '? '. Http_build_query ($ _ GET );}}}
GetPages (); // generate a page number style (you can add a custom style) // A total of 45 records are displayed for each style, with 10 records per page, current page 1/4 [Homepage] [Previous Page] [1] [2] [3] .. [Next Page] [last Page] echo $ p-> showPages (1 );