PHP Pagination Class Example analysis

Source: Internet
Author: User
Tags ord
This article mainly shared a perfect PHP paging class, with a certain reference value, interested in small partners can refer to, hope to help everyone.

The example of this article for everyone to share the PHP page of the specific code, for your reference, the specific content as follows


<?php/** file:page.class.php Perfect Paging Class page */class Page {private $total;             Total number of records in the data table private $listRows;              Each page shows the number of rows private $limit;               The SQL statement uses the LIMIT clause to restrict the number of records to the private $uri;             Automatically get URL request address private $pageNum;              Total pages Private $page;         Current page Private $config = Array (' head ' = ' "Record", ' prev ' = ' "prev", ' next ' = "Next",               ' First ' = ' homepage ', ' last ' and ' end ');           Display content in the paging information, you can set the private $listNum = 10 by the set () method; The number of default page list display/** construction method, you can set the properties of the paging class @param int $total calculate the total number of records for the paging @param int $listRows optional, set each page to display the The number of records, the default is 25 @param mixed $query optional, to pass parameters to the target page, can be an array or query string format @param bool $ord Optional, the default value is true, the page starts from the first page , False is the last page */Public function __construct ($total, $listRows =25, $query = "", $ord =true) {$this->total = $to      Tal  $this->listrows = $listRows;    $this->uri = $this->geturi ($query);      $this->pagenum = ceil ($this->total/$this->listrows);      /* The following judgment is used to set the current face */if (!empty ($_get["page")) {$page = $_get["page"];        }else{if ($ord) $page = 1;      else $page = $this->pagenum;        if ($total > 0) {if (Preg_match ('/\d/', $page)) {$this->page = 1;        }else{$this->page = $page;      }}else{$this->page = 0;    } $this->limit = "Limit". $this->setlimit (); The/** is used to set the display of paging information, which can be manipulated @param string $param is the subscript of the member Property array config @param string $value used to set the config subscript The corresponding element value @return object returns its own $this, used for the use of the operation */function set ($param, $value) {if (Array_key_exists ($PA      RAM, $this->config)) {$this->config[$param] = $value;    } return $this; }/* is not a direct call, this method allows you to use the value directly outside the object for the private member property limit and Page */function __get ($args) {if ($args = = "Limit" | |      $args = = "page") return $this-$args;    else return null;     /** output Paging @param int 0-7 as a parameter for customizing the output paging structure and the order of the structure, by default outputting all structures @return String paging information content      */function Fpage () {$arr = Func_get_args ();      $html [0] = "<span class= ' p1 ' > Total <b> {$this->total} </b>{$this->config[" Head "} </span>"; $html [1] = "Page <b>". $this->disnum (). "      </b> article ";      $html [2] = "This page from <b>{$this->start ()}-{$this->end ()}</b> bar";      $html [3] = "<b>{$this->page}/{$this->pagenum}</b> page";      $html [4] = $this->firstprev ();      $html [5] = $this->pagelist ();      $html [6] = $this->nextlast ();            $html [7] = $this->gopage (); $fpage = ' <p style= ' font:12px \ ' \5b8b\4f53\ ', San-serif; "      > ';            if (count ($arr) < 1) $arr = Array (0, 1,2,3,4,5,6,7); for ($i = 0; $i < count ($arr), $i + +) $fpage. = $html [$arr [$i]];      $fpage. = ' </p> ';    return $fpage; }/* Private method used inside the object, */Private Function Setlimit () {if ($this->page > 0) return ($this->page-1      ) * $this->listrows. ", {$this->listrows}";    else return 0;        }/* Private method used inside the object to automatically get access to the current URL */Private function GetURI ($query) {$request _uri = $_server["Request_uri"]; $url = Strstr ($request _uri, '? ')?            $request _uri: $request _uri. '? ';      if (Is_array ($query)) $url. = Http_build_query ($query);          else if ($query! = "") $url. = "&". Trim ($query, "?&");      $arr = Parse_url ($url);        if (Isset ($arr ["Query"])) {parse_str ($arr ["Query"], $arrs);        unset ($arrs ["page"]); $url = $arr ["path"]. '? '.      Http_build_query ($ARRS); } if (Strstr ($url, '? '))          {if (substr ($url,-1)! = '? ') $url = $url. '      & ';      }else{$url = $url. '? ';    } return $url; }/* Private method used inside the object to get the number of records starting at the current page*/Private Function Start () {if ($this->total = = 0) return 0;    else return ($this->page-1) * $this->listrows+1; }/* Private method used inside the object to get the number of records at the end of the current page */Private Function End () {return min ($this->page * $this->listrows, $thi    S->total); }/* Private method used inside the object to get operation information for the previous and first page */Private Function Firstprev () {if ($this->page > 1) {$str = "        <a href= ' {$this->uri}page=1 ' >{$this->config["First"]}</a> "; $str. = "<a href= ' {$this->uri}page=". ($this->page-1). "'            >{$this->config["prev"]}</a> ";      return $str;            }/* Private method used inside the object to get the page number table information */Private function pagelist () {$linkPage = "<b>";      $inum = Floor ($this-&GT;LISTNUM/2);        /* The list in front of the current page */for ($i = $inum; $i >= 1; $i-) {$page = $this->page-$i;      if ($page >= 1) $linkPage. = "<a href= ' {$this->uri}page={$page} ' >{$page}</a>";   }   /* Information for the current page */if ($this->pagenum > 1) $linkPage. = "<span style= ' padding:1px 2px;background: #BBB; colo            R:white ' >{$this->page}</span> ";        /* The list after the current page */for ($i =1; $i <= $inum; $i + +) {$page = $this->page+ $i;        if ($page <= $this->pagenum) $linkPage. = "<a href= ' {$this->uri}page={$page} ' >{$page}</a>";      else break;      } $linkPage. = ' </b> ';    return $linkPage;        }/* Private method used inside the object to get operation information for the next and last page */Private Function Nextlast () {if ($this->page! = $this->pagenum) { $str = "<a href= ' {$this->uri}page=". ($this->page+1). "'        >{$this->config["Next"]}</a> "; $str. = "<a href= ' {$this->uri}page=". ($this->pagenum). "'        >{$this->config["last"]}</a> ";      return $str; }/* Private method used inside the object to display and process the form jump page */Private Function gopage () {if ($this->pagenum > 1) {Retu RN ' <input style= "width:20px;height:17px!important;height:18px;border:1px solid #CCCCCC;" type= "text" onkeydown= "Javascript:if ( event.keycode==13) {var page= (this.value> '. $this->pagenum. ')? '. $this->pagenum ': this.value;location=\ '. $this->uri. ' page=\ ' +page+\ ' \ '} "value=" '. $this->page. ' ><input style= "cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type= "button" value= "GO" onclick= "Javascript:var page= (this.previoussibling.value> '. $this->pagenum. ')? '.      $this->pagenum ': this.previoussibling.value;location=\ '. $this->uri. ' page=\ ' +page+\ ' \ ' > '; }/* Private method used inside the object to get the number of record bars displayed on this page */Private Function Disnum () {if ($this->total > 0) {return $t      His->end ()-$this->start () +1;      }else{return 0; }    }  }

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.