Originally published a Separate page class from the database, recently used when found that sometimes the search is too large, the use of the URL to pass parameters, may have a certain impact or known bugs, this time to do some expansion, while compatible with the previous model, using the above very simple, Only need to set more than one parameter on the code as follows:
Copy Code code as follows:
<?php
/**
* Function: Pagination class, according to the amount of data provided and page size
* Date Created: Fri Apr 16:45:21 CST 2007
* Last update:
* Author: Sanshi <sanshi0815@tom.com>
*/
class pagination
{
var $result = array ();
var $pVar = "MYP";//page argument paging count
var $urlParamStr = ""; All parameters of the page
var $sqlMoveNumber = 0; Offset of data
var $is _post = false;
Public function pagination ()
{
}
Public function set ($sum, $pageSize, $page = "", $is _post = False)
{
$this->is_post = $is _post;
$this->pvar = defined (' Page_bar ')? Page_bar: ' MYP ';
$url = $_server[' php_self ']. $_server[' query_string '];
Calculate the total number of pages
$curPage = $this->getcurpage ();
$temp [' sumpage '] = ceil ($sum/$pageSize);
Bug modified with page number exceeded
if ($curPage > $temp [' sumpage ']) $curPage = $temp [' Sumpage '];
$temp [' curpage '] = $curPage;
$temp [' nextPage '] = $temp [' sumpage '] > $curPage? ($curPage + 1): $temp [' sumpage '];
$temp [' uppage '] = $curPage >1? $curPage-1:1;
$temp [' endpage '] = $temp [' Sumpage '];
$this->urlparamstr = $this->_makeurl ($url);
$temp [' urlpagestr '] = ($page = = "")? $this->_makeurl ($url): $page. $this->urlparamstr;
$this->sqlmovenumber = $curPage <2? 0: ($curPage-1) * $pageSize;
$temp [' sqlmovenumber '] = $this->sqlmovenumber;
$temp [' rowno '] = $sum;
$this->result = $temp;
return $this->result;
Print_r ($temp); exit ();
}
Public Function Getsqlmove ()
{
return $this->sqlmovenumber;
}
Public Function Getfooter ()
{
$str = "A total of <font color= #FF0033 >{$this->result[' Rowno ']}</font>, <a href= ' {$this->result[' Urlpagestr ']}{$this->result[' uppage ']} ';
if ($this->is_post) $str. = "Onclick= ' return Sanshi_page_post (this.href,{$this->result[' uppage '}); ' ";
$str. = "> Prev </a>";
$str. = "Current {$this->result[' curpage ']} page";
$str. = "Altogether {$this->result[' sumpage ']} page";
$str. = "<a href= ' {$this->result[' urlpagestr ']}{$this->result[' nextPage ']} '";
if ($this->is_post) $str. = "Onclick= ' return Sanshi_page_post (this.href,{$this->result[' nextPage '}); ' ";
$str. = "> Next page </a>";
return $str;
}
Public Function Getjumppage ()
{
if ($this->is_post)
{
$str = "Skip to <select name= ' topage ' size= ' 1 ' onchange= ' Sanshi_page_post (\" {$this->result[' urlpagestr ']}\ ') + This.value,this.value) ' >\n ';
}else{
$str = "Skip to <select name= ' topage ' size= ' 1 ' onchange= ' window.location=\ '" {$this->result[' urlpagestr ']}\ ' + This.value ' >\n ';
}
For ($i =1 $i <= $this->result[' sumpage '); $i + +)
{
$str. = ($i = = $this->result[' curpage '])? "<option value= ' $i ' selected> $i </option>\n": <option value= ' $i ' > $i </option>\n ';
}
$str. = "</select> page";
Output Form form
if ($this->is_post)
{
$str. = "<script type= text/javascript" ><!--
function Sanshi_page_post (jump_link,page_no)
{
document.getElementById (' Sanshi_gopage '). Action = Jump_link; document.getElementById (' {$this->pvar} '). value = Page_no;
document.getElementById (' Sanshi_gopage '). Submit ();
return false;
}
--></script> ";
$str. = "<form name= ' sanshi_gopage ' id= ' sanshi_gopage ' method=post> '";
$str. = "<input type=\" hidden\ "Name= ' {$this->pvar} ' id= ' {$this->pvar} ' value=0> ';
$_post = Isset ($_post)? $_post:array ();
foreach ($_post as $k => $v)
{
$str. = "<input type=\" hidden\ "name=\" {$k}\ "id=\" {$k}\ "value=\" {$v}\ ">";
}
$str. = "</form>";
}
return $str;
}
Public Function Getcurpage ()
{
return Isset ($_get[$this->pvar])? Intval ($_get[$this->pvar]): 1;
}
Parse the parameter return of the URL? After the argument, the page number is empty including the question mark
Private Function _makeurl ($url)
{
$arrayUrl =parse_url ($url);
if (Isset ($arrayUrl [' Query '])
{
$q = $arrayUrl [' query '];
Parse_str ($q, $qParam);
Print_r ($qParam);
if (array_key_exists ($this->pvar, $qParam))
{
foreach ($qParam as $k => $v)
{
if ($k!= $this->pvar) $temp [$k] = $v;
}
$temp [$this->pvar] = "";
if (function_exists ("Http_build_query"))
Return '? '. Http_build_query ($temp);
Else
Return '? '. Sanshi_http_build_query ($temp);
}else {
Return "? {$q}&{$this->pvar}= ";
}
}else{
Return "? {$this->pvar}= ";
}
}
}
function Sanshi_http_build_query ($a, $b = ', $c =0)
{
if (!is_array ($a)) return false;
foreach ((array) $a as $k => $v)
{
if ($c)
$k = $b. " [". $k."] ";
ElseIf (Is_int ($k))
$k = $b. $k;
if (Is_array ($v) | | Is_object ($v))
{
$r []=http_build_query ($v, $k, 1);
Continue
}
$r []= $k.] = ". UrlEncode ($v);
}
Return implode ("&", $r);
}
/*
Print_r ($_server);
$p = new pagination ();
$p->set (10,2, ' thi.php ');
echo $p->getfooter ();
echo $p->getjumppage ();
*/
?>