麻辣堂|资源主站|开发论坛|在线手册
首页 Apache Linux Java MySQL 注册帮助 
PHP项目开发组是PHP开发资源网于2007组年建成立的项目开发团队,目前核心开发成员有27人, 项目协作成员8名.下设7个开发组,主要承接大/中型网站项目开发任务。

    由于开发任务较多,人员比较紧张,现面向社会招聘全职或者兼职开发人员,不管你是在校大学生,还是全职开发人员,以及SOHO都可以联系本站,我们可以长期合作,并为您带来丰厚的报酬。
  您现在的位置:PHP开发资源网 > 麻辣堂 > 详细资料
待解决
只需两个参数:最傻瓜的一个通用分页类
悬赏分:20 - 2007年08月21日

<?php

/**
** 通用分页类。只需提供数据总数与每页显示数。
** 无需指定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);

提问者:linolu   08-21 15:03
答复
路过。。。顺便帮顶:)
回答者:玉米づ冰冻可乐 - 瓦岗村民 8-22 09:10
我也来回答:
不管你有没有帮助我们,瓦岗寨8万村民将感谢你。。。。。

为防止灌水,您需要计算一道数学题: 答案:
86 + 81 = ? 请将计算结果填在上面

 
[]
©2007 PhpRes.COM