php google或baidu分頁代碼_php技巧

來源:互聯網
上載者:User

複製代碼 代碼如下:

<?php
/**

作者:瀟湘部落格

時間:
2009-11-26

php技術群:
37304662

使用方法:
include_once'Pager.class.php';
$pager=new Pager();
if(isset($_GET['page']))
$pager->setCurrentPage($_GET['page']);
else
$pager->setCurrentPage(1);

$pager->setRecorbTotal(1000);
$pager->setBaseUri("page.php?");
echo $pager->execute();

**/
class Pager{
/**
*int總頁數
**/
protected $pageTotal;
/**
*int上一頁
**/
protected $previous;
/**
*int下一頁
**/
protected $next;
/**
*int中間頁起始序號
**/
protected $startPage;
/**
*int中間頁終止序號
**/
protected $endPage;
/**
*int記錄總數
**/
protected $recorbTotal;
/**
*int每頁顯示記錄數
**/
protected $pageSize;
/**
*int當前顯示頁
**/
protected $currentPage;
/**
*string基url地址
**/
protected $baseUri;

/**
*@returnstring擷取基url地址
*/
public function getBaseUri(){
return$this->baseUri;
}

/**
*@returnint擷取當前顯示頁
*/
public function getCurrentPage(){
return $this->currentPage;
}

/**
*@returnint擷取每頁顯示記錄數
*/
public function getPageSize(){
return $this->pageSize;
}

/**
*@returnint擷取記錄總數
*/
public function getRecorbTotal(){
return$this->recorbTotal;
}

/**
*@paramstring$baseUri設定基url地址
*/
public function setBaseUri($baseUri){
$this->baseUri=$baseUri;
}

/**
*@paramint$currentPage設定當前顯示頁
*/
public function setCurrentPage($currentPage){
$this->currentPage=$currentPage;
}

/**
*@paramint$pageSize設定每頁顯示記錄數
*/
public function setPageSize($pageSize){
$this->pageSize=$pageSize;
}

/**
*@paramint$recorbTotal設定擷取記錄總數
*/
public function setRecorbTotal($recorbTotal){
$this->recorbTotal=$recorbTotal;
}

/**
*建構函式
**/
public function __construct()
{
$this->pageTotal=0;
$this->previous=0;
$this->next=0;
$this->startPage=0;
$this->endPage=0;

$this->pageSize=20;
$this->currentPage=0;
}

/**
*分頁演算法
**/
private function arithmetic(){
if($this->currentPage<1)
$this->currentPage=1;

$this->pageTotal=floor($this->recorbTotal/$this->pageSize)+($this->recorbTotal%$this->pageSize>0?1:0);

if($this->currentPage>1&&$this->currentPage>$this->pageTotal)
header('location:'.$this->baseUri.'page='.$this->pageTotal);

$this->next=$this->currentPage+1;
$this->previous=$this->currentPage-1;

$this->startPage=($this->currentPage+5)>$this->pageTotal?$this->pageTotal-10:$this->currentPage-5;
$this->endPage=$this->currentPage<5?11:$this->currentPage+5;

if($this->startPage<1)
$this->startPage=1;

if($this->pageTotal<$this->endPage)
$this->endPage=$this->pageTotal;
}

/**
*分頁樣式
**/


protected function pageStyle(){
$result="共".$this->pageTotal."頁";

if($this->currentPage>1)
$result.="<a href=\"".$this->baseUri."page=1\"><font style=\"font-family:webdings\">第1頁</font></a> <a href=\"".$this->baseUri."page=$this->previous\"><fontstyle=\"font-family:webdings\">前一頁</font></a>";
else
$result.="<font style=\"font-family:webdings\">第1頁</font> <font style=\"font-family:webdings\"></font>";

for($i=$this->startPage;$i<=$this->endPage;$i++){
if($this->currentPage==$i)
$result.="<font color=\"#ff0000\">$i</font>";
else
$result.=" <a href=\"".$this->baseUri."page=$i\">$i</a> ";
}

if($this->currentPage!=$this->pageTotal){
$result.="<a href=\"".$this->baseUri."page=$this->next\"><font style=\"font-family:webdings\">後一頁</font></a> ";
$result.="<a href=\"".$this->baseUri."page=$this->pageTotal\"><font style=\"font-family:webdings\">最後1頁</font></a>";
}else{
$result.="<font style=\"font-family:webdings\">最後1頁</font> <font style=\"font-family:webdings\"></font>";
}
return $result;
}



/**
*執行分頁
**/
public function execute(){
if($this->baseUri!=""&&$this->recorbTotal==0)
return"";
$this->arithmetic();
return $this->pageStyle();
}
}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.