PHP adaptive paging Code allows you to flexibly customize CSS styles and paging link effects!

Source: Internet
Author: User
Original! None? Options that can be customized by php *** can be customized: Page Link display effect, the style of the current page link button, and the name of the page value obtained in the URL, you can include your own parameters ** usage: * 1. You need to input parameters during class initialization. The type is array. * Array (* (required) totalRows

Original! None? Php/*** customizable paging class ** options include pagination link display effect, the style of the current page link button, and the name of the pagination value obtained in the URL, you can include your own parameters ** usage: * 1. You need to input parameters during class initialization. The type is array. * Array (* (required) 'totalrows'

Original! <无>
  '20140901', total number of data items to be displayed; * (required) 'pagesize' => '2', number of codes to be displayed per page; * (required) 'currentpage' => $ _ GET ['P'], current page number, which can be obtained by default through $ _ GET ['P']. The name p can be customized * (required) 'baseurl' => '/welcome? Id = 3', the link address of your current page, for example http://www.xxx.com/test.php (Or/test. php). If it is followed by a parameter, it can be http://www.xxx.com/test?id=8 * (Optional, default value: 3) 'offset' => '3'. The offset between the left and right of the current page. For example, if the current page number is 5, several numeric links are displayed on the left and right sides of 5, the default value is 3, and the effect is 2, 3, 4, 5, 6, 7, 8 * (optional, default value: p) 'pagestring' => 'P ', GET the name of the current page number through $ _ GET ['P']. The default value is p * (optional, the default value is here) 'classname' => 'where ', the style of the current page link button. The default style is here, so you can write the css style like this. here {background: # FF4500;}) ** 2. Available methods. * A. After the initialization class, you need to call the pagination ([$ style = '1'] [, $ output = TRUE]) method to generate A paging link * about the parameter description: * @ param $ style (default value: 1, not filled in): Get all components of the link, that is, Home Page + Previous Page + digital link + next page + last page * @ param $ style = 2: only get the number link * @ param $ style = 3: only get the previous page + next page * @ param $ style = 4: only get the previous page + number link + next page, (excluding the first and last pages) ** @ param $ output (TRUE by default). If the return pagination link * @ param $ output is FALSE, directly output the paging links ** B and getCurrentPage () to obtain the current page number. If the current page number is determined based on the authenticity of the page, the user cannot enter an error. For example: http://www.xxx.com/test?p=-100 In this case, the current page number is 1 ** C, pageAmount (), and the total page number is obtained ** @ author @ * @ link http://blog.sina.com.cn/yanyinghq **/Class Page {private $ pageSize; // The number of list entries displayed on each Page of your website. private $ totalRows; // The total number of records returned by the database query is private $ url; // reference URLprivate $ pageAmount; // the total number of pages private $ currentPage; // the current page number private $ offset = 4; // the page number offset private $ pageString = 'P '; // The name of the page number in the URL is private $ classHere = 'class = "here" '; // The class Style class Name of the current page link. The default value is here // initialize the current page number, total number of records, number of records per page public function _ construct ($ param) {$ this-> pageSize = $ param ['pagesize']; $ this-> tot AlRows = $ param ['totalrows ']; $ this-> url = $ param ['baseurl']; $ this-> offset =! Empty ($ param ['offset'])? $ Param ['offset']: $ this-> offset; $ this-> pageString =! Empty ($ param ['pagestring'])? $ Param ['pagestring']: $ this-> pageString; $ this-> classHere =! Empty ($ param ['classname'])? $ Param ['classname']: $ this-> classHere; $ this-> currentPage = (int) $ param ['currentpage'];} /*** create a paging link ** @ param $ style is 1 by default: Get all components of the link * @ param $ style = 2: only get the number link * @ param $ style = 3: only get the previous page, next page * @ param $ style = 4: only get the previous page, next page, and number link, if ** @ param $ output is set to TRUE and the paging link * @ param $ output is returned to be set to FALSE, directly output the paging link **/public function pagination ($ style = '1', $ output = TRUE) {$ this-> baseUrl (); $ this-> pageAmount (); $ This-> currentPage (); // obtain all components if ($ style = '1') {$ page = $ this-> indexPage (). $ this-> prevPage (). $ this-> pageNumber (). $ this-> nextPage (). $ this-> endPage ();} else if ($ style = '2') {// get a pure digital link $ page = $ this-> pageNumber ();} else if ($ style = '3') {// only get the next page of the previous page $ page = $ this-> prevPage (). $ this-> nextPage ();} else if ($ style = '4') {// Previous page, next page, numeric link $ page = $ this-> prevPage (). $ this-> pageNumber (). $ this-> nextPage ();} if ($ outpu T) {return $ page;} else {echo $ page;}/*** get the current page ** @ return Current page number, */public function getCurrentPage () {$ this-> pageAmount (); $ this-> currentPage (); return $ this-> currentPage ;} /*** calculate all pages ** this method can be called outside the class to return the total number of pages ** @ return the total number of pages */public function pageAmount () {$ this-> pageAmount = ceil ($ this-> totalRows/$ this-> pageSize); if ($ this-> pageAmount <= 0) {$ this-> pageAmount = '1';} return $ this-> pageAmoun T;}/*** determine whether the reference link carries the parameter ** the reference link submits the current page number link for the user ** if the parameter is included, add & p = ** after the link. If no parameter is included, add? P = */private function baseUrl () {if (preg_match ('/\? /', $ This-> url) {$ this-> url = $ this-> url. '&'. $ this-> pageString. '=';} else {$ this-> url = $ this-> url. '? '. $ This-> pageString. '=';}/*** verify the authenticity of the current page ** if the current page number is less than 1 or does not exist, the default current page number is 1 ** if the current page number is greater than the total number of pages, by default, the current page number is the total number of pages **/private function currentPage () {if ($ this-> currentPage <1 |! $ This-> currentPage) {$ this-> currentPage = 1;} else if ($ this-> currentPage> $ this-> pageAmount )) {$ this-> currentPage = $ this-> pageAmount;}/*** homepage link */private function indexPage () {if ($ this-> currentPage = 1) return; return 'url. '1 "> homepage ';}/*** link to the last page */private function endPage () {if ($ this-> currentPage ==$ this-> pageAmount) return; return 'url. $ this-> pageAmount. '"> last page';}/*** previous page */private function prevPage () {If ($ this-> currentPage = 1) return; return 'url. ($ this-> currentPage-1 ). '"> Previous Page';}/*** next page */private function nextPage () {if ($ this-> currentPage ==$ this-> pageAmount) return; return 'url. ($ this-> currentPage + 1 ). '"> next page';}/*** link to the intermediate page number **/private function pageNumber () {$ left =" "; $ right = ""; // if the total number of records is greater than the number of all links, if ($ this-> pageAmount> ($ this-> offset * 2 + 1 )) {// distance between the current page number and the Home Page $ leftNum = $ this-> curr EntPage-1; // The distance between the current page number and the end page $ rightNum = $ this-> pageAmount-$ this-> currentPage; // when the current page number is offset less than the homepage distance, fill in the missing small square if ($ leftNum <$ this-> offset) on the right) {// link on the left ($ I = $ leftNum; $ I >=1; $ I --) {$ left. = 'url. ($ this-> currentPage-$ I ). '"> '. ($ this-> currentPage-$ I ). '';} // link to the right ($ j = 1; $ j <= ($ this-> offset * 2-$ leftNum); $ j ++) {$ right. = 'url. ($ this-> currentPage + $ j ). '"> '. ($ this-> currentPage + $ J ). '';} else if ($ rightNum <$ this-> offset) {// link on the left ($ I = ($ this-> offset * 2-$ rightNum); $ I >=1; $ I --) {$ left. = 'url. ($ this-> currentPage-$ I ). '"> '. ($ this-> currentPage-$ I ). '';} // the link to the right for ($ j = 1; $ j <= $ rightNum; $ j ++) {$ right. = 'url. ($ this-> currentPage + $ j ). '"> '. ($ this-> currentPage + $ j ). '';}} else {// link to the left of the current link for ($ I = $ this-> offset; $ I >=1; $ I --) {$ left. = 'url. ($ this-> currentPag E-$ I ). '"> '. ($ this-> currentPage-$ I ). '';} // link to the right of the current link for ($ j = 1; $ j <= $ this-> offset; $ j ++) {$ right. = 'url. ($ this-> currentPage + $ j ). '"> '. ($ this-> currentPage + $ j ). '';} return $ left. 'url. $ this-> currentPage. '"class =" here "> '. $ this-> currentPage. ''. $ right;} else {$ allLink = ''; // when the total number of pages is less than the number of links to be displayed, all are displayed for ($ j = 1; $ j <= $ this-> pageAmount; $ j ++) {$ allLink. = 'url. $ j. '"'. ($ j = $ this-> currentPage? $ This-> classHere: ''). '>'. $ j.'';} return $ allLink ;}}}
  Untitled document
  '20140901', 'pagesize' => '2', 'currentpage' = >@$ _ GET ['P'], 'baseurl' => '/page_index.php? Id = 3'); $ page1 = new Page ($ param); $ page2 = new Page ($ param); $ page3 = new Page ($ param ); $ page4 = new Page ($ param); $ page5 = new Page ($ param); echo 'total records: 6666'; echo'
  '; Echo' 2 records per page
  '; Echo 'current page number:'. $ page1-> getCurrentPage ().'
  '; Echo' total '. $ page1-> pageAmount ().' page
  '; Echo'
  • '. $ Page1-> pagination ().'
  • '; Echo'
  • '. $ Page2-> pagination ('1 ').'
  • '; // The default value is 1, so the effect is the same as that of the unspecified echo'
  • '. $ Page3-> pagination ('2 ').'
  • '; Echo'
  • '. $ Page4-> pagination ('3 ').'
  • '; Echo'
  • '. $ Page5-> pagination ('4 ').'
  • ';?>

    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.