PHP implements customizable paging classes

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces the pagination class for PHP to implement custom styles and customize pagination styles, if you are interested, you can refer to the examples in this article to share with you the pagination class for PHP implementation and custom styles. the specific content is as follows:

<? Php // namespace Component;/*** 2016-3-27 * @ author ankang */class Page {private $ ShowPage; private $ CountPage; private $ Floorp; private $ PageUrl; private $ PageClass; private $ CurClass; /*** @ author ankang * @ param number $ CountNum total data * @ param string $ PageUrl jump link * @ param string $ PageClass label overall style * @ param string $ PageUrl current page style * @ param number $ PageSize number of data entries displayed on each page * @ param number $ ShowPage Page number */public function _ construct ($ CountNum, $ PageUrl = NULL, $ PageClass = NULL, $ CurClass = NULL, $ PageSize = 20, $ ShowPage = 5) {$ this-> ShowPage = $ ShowPage; $ this-> CountPage = ceil ($ CountNum/$ PageSize); $ this-> Floorp = floor ($ ShowPage/2 ); // offset $ this-> PageClass = is_null ($ PageClass )? '': $ PageClass; $ this-> CurClass = is_null ($ CurClass )? '': $ CurClass; // $ ServerURL = (preg_match ('/\? /I ', $ _ SERVER ['request _ URI'])? Preg_replace ('/\ & p \ = [0-9] +/I', "", $ _ SERVER ['request _ URI ']): $ _ SERVER ['request _ URI ']. "? "; // If (substr ($ ButURL, 0, 2) = '//') {// $ ServerURL = substr ($ ServerURL, 1 ); ///} // $ url = preg_replace ('/p = [\ d] */I', '', $ ServerURL); $ url = ''; // We recommend that you upload the url by yourself. if you do not upload the url, you can open the above code to automatically obtain $ this-> PageUrl = is_null ($ PageUrl )? $ Url: $ PageUrl;}/***** @ param number $ Page * @ param string $ ShowToPage * homepage, top and bottom pages, and last Page * @ param string $ Html tag element, li, p * @ return string */public function getPage ($ Page = 1, $ ShowToPage = true, $ Html = null) {$ StartPage = ($ Page-$ this-> Floorp); // Start Page number $ EndPage = ($ Page + $ this-> Floorp ); // end page number if ($ this-> CountPage <$ this-> ShowPage) {$ StartPage = 1; $ EndPage = $ this-> CountPage;} if ($ StartP Age <1) {$ StartPage = 1; $ EndPage = $ this-> ShowPage;} if ($ EndPage> $ this-> CountPage) {$ StartPage = $ this-> CountPage-$ this-> ShowPage + 1; $ EndPage = $ this-> CountPage;} $ PageHtml = ''; if (! Is_null ($ Html) {if ($ Html = 'Lil') {$ Shtml ='
  • '; $ Ehtml ='
  • ';} Else {$ Shtml ='

    '; $ Ehtml ='

    ';}} If (true ==$ ShowToPage) {$ PageHtml. = "$ ShtmlPageUrl} p = 1 '> «homepage $ Ehtml"; $ PrveUrl = $ this-> getPrve ($ Page); $ PageHtml. = "$ Shtml» $ Ehtml"; $ PageHtml. = "$ ShtmlPageUrl} p = {$ this-> CountPage} '> Last Page» $ Ehtml";} return $ PageHtml;} public function getPrve ($ Page) {if ($ Page! = 1) {$ Prve = $ Page-1; $ PrveUrl = "{$ this-> PageUrl} p = {$ Prve }";} else {$ PrveUrl = "{$ this-> PageUrl} p = 1";} return $ PrveUrl;} public function getNext ($ Page) {if ($ Page! = $ This-> CountPage) {$ Next = $ Page + 1; $ NextUrl = "{$ this-> PageUrl} p = {$ Next }";} else {$ NextUrl = "{$ this-> PageUrl} p = {$ this-> CountPage}";} return $ NextUrl ;}}

    I would like to share with you one that is mainly used for beginners to learn php pages. the code is simple and practical, and the comments are complete.

    1. Page. class. php

    <? Php/*** paging class ** call method: * $ p = new Page (total Page number, displayed Page number, current Page number, number of entries per Page, [link]); * print_r ($ p-> getPages (); // generates 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 current page, and the number of entries per page, connect to write a method to generate a one-dimensional array, the key is the page number value for the connection to write a method to return a page number that generates a good style (and you can add a style as needed) the default style contains 45 records and 10 records are displayed on each page. Currently, 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 protected $ countPages; // total page number protected $ currPage; // The 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; $ t His-> subPages = $ subPages; // Obtain the current connection if (empty ($ href) if the link is not set )) {$ this-> href = htmlentities ($ _ SERVER ['php _ SELF ']);} else {$ this-> href = $ href ;} $ this-> construct_Pages ();}/*** getPages returned page number array * @ return array one-dimensional array key is the page number value link */public function getPages () {return $ this-> page_arr ;} /*** showPages: return the generated page number * @ param int $ style * @ return string generated page number */public function showPages ($ style = 1) {$ func = 'pagest Ry '. $ 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 ). '"> 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 ). '"> 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-> currPage <$ this-> countPages) {$ _ GET ['Page'] = $ this-> currPage + 1; $ pageStr. = '[href. '? '. Http_build_query ($ _ GET ). '"> Next page]';} $ _ GET ['Page'] = $ this-> countPages; $ pageStr. = '[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 = m In ($ right, $ this-> 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 side and calculate the left side. you must calculate the for ($ I = $ left; $ I <= $ right; $ I ++) twice) {$ _ GET ['Page'] = $ I; $ this-> page_arr [$ I] = $ this-> href. '? '. Http_build_query ($ _ GET );}}}
     

    2. demo. php

    <? Php/*** paging demo * Be the best of whatever you are! ** @ Author: Dzer <358654744@qq.com> * @ version: 17:38:23 * @ Last Modified time: 18:08:28 */header ("content-type: text/html; charset = utf8 "); include ('. /Page. class. php '); // introduce class // $ p = new Page (total Page number, displayed Page number, current Page number, number of entries per Page, [link]); // if the connection is not set, the current link $ page = isset ($ _ GET ['Page'])? $ _ GET ['Page']: 1; $ p = new page (, 7, $ Page, 8); // generates a page number array (the key is the page number, and the value is the link) echo"
    "; Print_r ($ p-> getPages (); // generate a page number style (custom styles can be added) // A total of 45 records are displayed for each style, 10 records are displayed, current page 1/4 [Homepage] [Previous Page] [1] [2] [3] .. [Next Page] [last Page] echo $ p-> showPages (1 );

    The above is all the content of this article. I hope it will help you learn PHP programming.

    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.