欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php google或baidu分页代码

程序员文章站 2023-11-22 16:07:46
复制代码 代码如下:

复制代码 代码如下:

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