PHP通用分頁類page.php[仿google分頁]

來源:互聯網
上載者:User

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;
}
}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.