/**
** 通用分页类。只需提供数据总数与每页显示数。
** 无需指定URL,链接由程序生成。方便用于检索结果分页。
** @author : lino(luckfeng@gmail.com)
** @site : http://www.ypren.com
** @version : 0.3
** @date : 2006/2/24
**/

class Pager{
var $url;
var $countall;
var $page;
var $thestr;
var $backstr;
var $nextstr;
var $pg;
//构造函数,实例化该类的时候自动执行该函数
function Pager($countall,$countlist){
@$this->pg=sprintf("%d",$_GET["pg"]);
//保证pg在未指定的情况下为从第1页开始
if ($this->pg==0){
$this->pg=1;
}
if (!isset($this->pg)){
$this->pg=1;
}
//记录数与每页显示数不能整队时,页数取余后加1
$this->countall = $countall;
if ($this->countall%$countlist!=0){
$this->page=sprintf("%d",$this->countall/$countlist)+1;
}
else{
$this->page=$this->countall/$countlist;
}

//得到当前的URL。具体实现请看最底部的函数实体
$this->url = Pager::getUrl();

//生成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->backstr.$this->thestr.$this->nextstr." 共".$this->countall." 条,每页".$countlist."条,分".$this->page."页");
}
//生成数字分页的辅助函数
function makepg($i,$pg){
if ($i==$pg){
return " ".$i."";
}
else{
return " ".$i."";
}
}
//生成上一页等信息的函数
function gotoback($pg){
if ($pg-1>0){
return $this->gotoback=" 首页 上页";
}
else{
return $this->gotoback="";
}

}
//生成下一页等信息的函数
function gotonext($pg,$page){
if ($pg < $page){
return " 下页 尾页";
}
else{
return "";
}
}

//处理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;
}
}
?>
*********************************************************
这里提供自用的一个引用示例~

$comment = new DoctorComment;
$pg = @$_REQUEST["pg"];
$pagelist = 10; //每页显示数
$limitFrom = 0;//开始limit的数
if (!isset($pg)){
$pg=1;
}
if ($pg>1){
$limitFrom = $pagelist*($pg-1);
}
else{
$limitFrom = 0;
}
//从mysql 数据库中取出记录集
$comment_arr = $comment->getDoctorCommentList($limitFrom,$pagelist);
//下面这行的功能是得到所有记录总数,也就是这段分页唯一要和数据库打交道的地方。
$commentNumber = $comment->countDoctorComment();

//在需显示处放下这行代码即可
$pager = new Pager($commentNumber,$pagelist);

附上稍加修改后用于XAJAX分页的分页函数


/**
** xajax通用分页类
** 适用于无刷新页面翻页
** 未提供注释,博君一笑~~
** @author : lino(luckfeng@gmail.com)
** @site : http://www.ypren.com
** @version : 0.2
** @date : 2006/2/16
**/

class Pager{
var $url;
var $countall;
var $page;
var $thestr;
var $backstr;
var $nextstr;
var $pg;
var $content;
var $function;
var $id;

function Pager($countall,$countlist,$id,$function,$thePage){ // 参数分别为:总数,每页显示数,操作的ID,操作的方法,当前页
$this->function = $function;
$this->id = $id;

if ($thePage==0 || $thePage==""){
$thePage = 1;
}
$this->pg = $thePage;
$this->countall = $countall;
if ($this->countall%$countlist!=0){
$this->page=sprintf("%d",$this->countall/$countlist)+1;
}
else{
$this->page=$this->countall/$countlist;
}

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);
$this->content = $this->backstr.$this->thestr.$this->nextstr." 共".$this->countall." 条,每页".$countlist."条,分".$this->page."页 当前第".$thePage."页";
return $this->content;
}

function makepg($i,$pg){
if ($i==$pg){
return " ".$i."";
}
else{
return " ".$i."";
}
}

function gotoback($pg){
if ($pg-1>0){
$pg = $pg-1;
return $this->gotoback=" 首页 上页";
}
else{
return $this->gotoback="";
}

}

function gotonext($pg,$page){
if ($pg < $page){
$pg = $pg+1;
return " 下页 尾页";
}
else{
return "";
}
}

}
?>