page.php
復制代碼 代碼如下:
<?php
/**
** 通用php分頁類。(仿Google樣式)
** 只需提供記錄總數與每頁顯示數兩個參數。(已附詳細使用說明..)
** 無需指定URL,鏈接由程序生成。方便用於檢索結果分頁。
** 表單采用GET方法提交,可保證在諸如查詢之,刪除之類的操作時,不丟失URL參數
**/
class Pager{
//IE地址欄地址
var $url;
//記錄總條數
var $countall;
//總頁數
var $page;
//分頁數字鏈接
var $thestr;
//首頁、上一頁鏈接
var $backstr;
//尾頁、下一頁鏈接
var $nextstr;
//當前頁碼
var $pg;
//每頁顯示記錄數量
var $countlist;
//翻頁樣式
var $style;
//構造函數,實例化該類的時候自動執行該函數
function Pager($countall,$countlist,$style="page"){
//記錄數與每頁顯示數不能整隊時,頁數取余後加1
$this->countall = $countall;
$this->countlist = $countlist;
$this->style=$style;
if ($this->countall%$this->countlist!=0){
$this->page=sprintf("%d",$this->countall/$this->countlist)+1;
}else{
$this->page=$this->countall/$this->countlist;
}
$this->pg=$_GET["pg"];
//保證pg在未指定的情況下為從第1頁開始
if (!ereg("^[1-9][0-9]*$",$this->pg) || empty($this->pg)){
$this->pg=1;
}
//頁碼超出最大范圍,取最大值
if ($this->pg>$this->page){
$this->pg=$this->page;
}
//得到當前的URL。具體實現請看最底部的函數實體
$this->url = Pager::getUrl();
//替換錯誤格式的頁碼為正確頁碼
if(isset($_GET["pg"]) && $_GET["pg"]!=$this->pg){
$this->url=str_replace("?pg=".$_GET["pg"],"?pg=$this->pg",$this->url);
$this->url=str_replace("&pg=".$_GET["pg"],"&pg=$this->pg",$this->url);
}
//生成12345等數字形式的分頁。
if ($this->page<=10){
for ($i=1;$i<$this->page+1;$i++){
$this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
}
}else{
if ($this->pg<=5){
for ($i=1;$i<10;$i++){
$this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
}
}else{
if (6+$this->pg<=$this->page){
for ($i=$this->pg-4;$i<$this->pg+6;$i++){
$this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
}
}else{
for ($i=$this->pg-4;$i<$this->page+1;$i++){
$this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
}
}
}
}
//生成上頁下頁等文字鏈接
$this->backstr = Pager::gotoback($this->pg);
$this->nextstr = Pager::gotonext($this->pg,$this->page);
//echo (" 共".$this->countall." 條,每頁".$this->countlist."條,共".$this->page."頁".$this->backstr.$this->thestr.$this->nextstr);
}
//生成數字分頁的輔助函數
function makepg($i,$pg){
if ($i==$pg){
return " <font class='".$this->style."'>".$i."</font>";
}else{
return " <a href=".Pager::replacepg($this->url,5,$i)." class='".$this->style."'><u>".$i."</u></a>";
}
}
//生成上一頁等信息的函數
function gotoback($pg){
if ($pg-1>0){
return $this->gotoback=" <a href=".Pager::replacepg($this->url,3,0)." class='".$this->style."'>首頁</a> <a href=".Pager::replacepg($this->url,2,0)." class='".$this->style."'>上一頁</a>";
}else{
return $this->gotoback="<span class='".$this->style."'>首頁 上一頁</span> ";
}
}
//生成下一頁等信息的函數
function gotonext($pg,$page){
if ($pg < $page){
return " <a href=".Pager::replacepg($this->url,1,0)." class='".$this->style."'>下一頁</a> <a href=".Pager::replacepg($this->url,4,0)." class='".$this->style."'>尾頁</a>";
}else{
return " <span class='".$this->style."'>下一頁 尾頁</span>";
}
}
//處理url中$pg的方法,用於自動生成pg=x
function replacepg($url,$flag,$i){
if ($flag == 1){
$temp_pg = $this->pg;
return str_replace("pg=".$temp_pg,"pg=".($this->pg+1),$url);
}else if($flag == 2) {
$temp_pg = $this->pg;
return str_replace("pg=".$temp_pg,"pg=".($this->pg-1),$url);
}else if($flag == 3) {
$temp_pg = $this->pg;
return str_replace("pg=".$temp_pg,"pg=1",$url);
}else if($flag == 4){
$temp_pg = $this->pg;
return str_replace("pg=".$temp_pg,"pg=".$this->page,$url);
}else if($flag == 5){
$temp_pg = $this->pg;
return str_replace("pg=".$temp_pg,"pg=".$i,$url);
}else{
return $url;
}
}
//獲得當前URL的方法
function getUrl(){
$url="http://".$_SERVER["HTTP_HOST"];
if(isset($_SERVER["REQUEST_URI"])){
$url.=$_SERVER["REQUEST_URI"];
}else{
$url.=$_SERVER["PHP_SELF"];
if(!empty($_SERVER["QUERY_STRING"])){
$url.="?".$_SERVER["QUERY_STRING"];
}
}
//在當前的URL裡加入pg=x字樣
if (!ereg("(pg=|PG=|pG=|Pg=)", $url)){
if (!strpos($url,"?")){
$url = $url."?pg=1";
}else{
$url = $url."&pg=1";
}
}
return $url;
}
}
?>