Pagination
<?php
/**
file:page.class.php
Perfect Paging Class page
*/
Class Page {
Private $total; Total number of records in the data table
Private $listRows; Number of rows displayed per page
Private $limit; The SQL statement uses the LIMIT clause to restrict the number of records fetched
Private $uri; Get URL Request address automatically
Private $pageNum; Total pages
private $page;//Current page
Private $config = Array (
' Head ' = ' record ',
' Prev ' = ' prev ',
' Next ' = ' next page ',
' First ' = ' home ',
' Last ' = ' End '
);
Display content in the paging information, you can set it yourself through the set () method
Private $listNum = 10; The number of default page list displays
/**
Construction method, you can set the properties of the paging class
Total number of records @paramint $total calculation of paging
@paramint $listRows Optional, set the number of records to display per page by default of 25
@parammixed $query Optional, to pass parameters to the target page, either an array or a query string format
@param bool$ord Optional, the default value is true, the page is displayed from the first page, False is the last page
*/
Public function __construct ($total, $listRows =25, $query = "", $ord =true) {
$this->total = $total;
$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 ();
}
/**
is used to set the display of paging information for coherent operation
@paramstring $param is the subscript for the member Property array config
@paramstring $value to set the element value for the config subscript The
@returnobject return this object to its own $this, for use in a habitual operation
*/
Function set ($param, $value) {
if (array_key_exists ($param, $this- >config) {
$this->config[$param] = $value;
}
return $this;
}
/* is not a direct call, this method allows you to use the value of the limit and page directly outside of the object to get the private member property, */
Function __get ($args) {
if ($args = = "Limit" | | $args = = "page")
return $this, $args;
else
return null;
}
/**
Output paging in specified format
@paramint0 7 numbers as parameters for customizing the output paging structure and order of the structure, default output all structure
@returnstring 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> Bar ";
$html [2] = " this page from <b>{$this->start ()}-{$this->end ()}</b> strip ";
$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 = ' <div 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. = ' </div> ';
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 get the current URL of the access automatically */
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, $this->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->LISTNUM/2);
/* list before 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 on current page */
if ($this->pagenum > 1)
$linkPage. = "<span style= ' padding:1px 2px;background: #BBB; Color:white ' >{$this->page}</span>& nbsp; ";
/* list following 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) {
Return ' <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 $this->end ()-$this->start () +1;
}else{
return 0;
}
}
}
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<body>
<!--total N Records current 1th page first prev 1,2,3,4,5,6,7,8,9,10 next last jump to page
Total 101 Data
Show 10 per page
Total pages 11 pages
Current Page 5
Limit (5-1) *10,10
$html [0] = "total N records";
$html [1] = "10 article on this page"
$html [2] = "This page from 41-50";
$html [3] = "5/10";
$html [4] = "First page prev";
$html [5] = "1,2,3,4,5,6"
$html [6] = "Next Page last";
$html [7] = "Jump to" page--
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Code </td>
<td> name </td>
<td> Parent Code </td>
</tr>
<?php
Include ("dbda.php");
Include ("page.class.php");
$db = new Dbda ();
$sqlall = "SELECT count (*) from chinastates";
$attrall = $db->query ($sqlall);
$total = $attrall [0][0];
$page = new Page ($total, 15); Required parameter 1. Total number of data bars
$sql = "SELECT * from Chinastates". $page->limit;//sql Statement Stitching limit
$attr = $db->query ($sql);
foreach ($attr as $v)
{
echo "<tr><td>{$v [0]}</td><td>{$v [1]}</td><td>{$v [2]}</td></tr> ";
}
?>
</table>
<?php
Paging information is called
echo "<div>". $page->fpage (). " </div> ";
?>
</body>
PHP 16th session