A-A+

PHP基础分页类

2012年06月08日 PHP技术文章 暂无评论 阅读 71 views 次

一个比较基础的PHP分页类,比较初级,不过比较实用,可以自定义分页的CSS样式,共包含三个文件,(1) PHP基础分页类 (2) style 样式文件,(3) 实例文件

在 PHP 分页类文件中,已经用过许多了,有非常完善的,也有简单的,甚至我做的一些网站当中,把分页的 PHP 代码直接做到了 PHP 页面里,这显然是很不规范的。

合理地利用好PHP的面向对象,可以节省许多时间,而对于层出不穷的PHP样式应该如何去使用,怎么选择去使用,主要还是根据自己的网站需求,本博建议如果不是特别大型的网站,真没必要去使用非常完善的PHP分页样式,因为好多功能基本上都是一个摆设。

下面是本博搜集的一个 PHP 基础分页类,最大的优点就是可以自定义分页的CSS样式,对于小型的 PHP 应用程序已经是足够的了。

1,PHP基础分页类:

<?php
/*
* 类名:page
* 功能:利用模板分页
*/
class Page{

var $CurrentPage;
var $LeftOffSet;
var $RightOffSet;

var $TotalPage;//总页数
var $RecordCount;//总记录数
var $PageSize;//每页显示条数

var $PageUrl;
var $HypeLink;

var $Template;
var $tpl;
var $TagItems=array();
var $TagValues=array();

var $SqlQuery;

//构造函数
function Page($currentpage=1,$pagesize=5,$leftoffset=2,$rightoffset=7,$pageurl="?page="){
echo "分页类开始";
$this->CurrentPage=ceil(abs(@$currentpage+0));
(emptyempty($this->CurrentPage))?$this->CurrentPage=1:$this->CurrentPage=$this->CurrentPage;
$this->PageSize=ceil(abs(@$pagesize+0));
(emptyempty($this->PageSize))?$this->PageSize=5:$this->PageSize=$this->PageSize;
$this->LeftOffSet=ceil(abs(@$leftoffset+0));
(emptyempty($this->LeftOffSet))?$this->LeftOffSet=2:$this->LeftOffSet=$this->LeftOffSet;
$this->RightOffSet=ceil(abs(@$rightoffset+0));
(emptyempty($this->RightOffSet))?$this->RightOffSet=7:$this->RightOffSet=$this->RightOffSet;
$this->PageUrl=$pageurl;

$this->SetDefaultTagValue();
}

//取得记录总数
//$sql="select count(id) as n from table";
function GetRecordCount($sql,$conn){
$query=@mysql_query($sql,$conn);
if(!$query){echo "执行sql语句失败";exit();}
while($rs=mysql_fetch_row($query)){
$this->RecordCount=$rs[0];//取得记录总数
}
$this->TotalPage=ceil($this->RecordCount / $this->PageSize);//计算总页数
if($this->CurrentPage > $this->TotalPage){$this->CurrentPage=$this->TotalPage;}//判断当前页是否大于总页数
mysql_free_result($query);
}

//select * from tb p->setlimit();
function SetLimit(){
$limit="LIMIT ".($this->CurrentPage-1)*$this->PageSize;
$limit.=",$this->PageSize";
return $limit;
}

function ExecuteSql($sql,$conn){
if(!$sql||!$conn){echo "参数传递错误";return false;}
$this->SqlQuery=mysql_query($sql,$conn);
if(!$this->SqlQuery){echo "执行sql语句失败";return false;}
}
function RecordSet(){
return mysql_fetch_array($this->SqlQuery);
}

//取得模板内容
function GetTemplate($filedir){
if(file_exists($filedir)){
$f=fopen($filedir,"r");
$this->Template=fread($f,filesize($filedir));
}else{
echo "获取模板文件失败...文件不存在";
exit();
}
//取得区块内容
$start=strpos($this->Template,"<!--templatebegin-->");
$end=strpos($this->Template,"<!--templateend-->");
$this->tpl=substr($this->Template,$start+strlen("<!--templatebegin-->"),$end-$start-strlen("<!--templateend-->")-2);
if($this->tpl==""){echo "模板内容为空,请检查标签设置是否正确。";exit();}
//echo $this->tpl;
}

//设定默认标签对应值
function SetDefaultTagValue(){
$this->TagItems["previouspage"]="上一页";
$this->TagItems["previouspagelink"]="<a href='{link}'>上一页</a>";
$this->TagItems["previoustenpage"]="上十页";
$this->TagItems["previoustenpagelink"]="<a href='{link}'>上十页</a>";
$this->TagItems["nextpage"]="下一页";
$this->TagItems["nextpagelink"]="<a href='{link}'>下一页</a>";
$this->TagItems["nexttenpage"]="下十页";
$this->TagItems["nexttenpagelink"]="<a href='{link}'>下十页</a>";
$this->TagItems["firstpage"]="首页";
$this->TagItems["firstpagelink"]="<a href='{link}'>首页</a>";
$this->TagItems["lastpage"]="尾页";
$this->TagItems["lastpagelink"]="<a href='{link}'>尾页</a>";
$this->TagItems["listpage"]="[{list}]";
$this->TagItems["listpagelink"]="<a href='{link}'>[{list}]</a>";

//定义模板标签
$this->TagValues["previouspage"]="{previouspage}";
$this->TagValues["previouspagelink"]="{previouspage}";
$this->TagValues["previoustenpage"]="{previoustenpage}";
$this->TagValues["previoustenpagelink"]="{previoustenpage}";
$this->TagValues["nextpage"]="{nextpage}";
$this->TagValues["nextpagelink"]="{nextpage}";
$this->TagValues["nexttenpage"]="{nexttenpage}";
$this->TagValues["nexttenpagelink"]="{nexttenpage}";
$this->TagValues["firstpage"]="{firstpage}";
$this->TagValues["firstpagelink"]="{firstpage}";
$this->TagValues["lastpage"]="{lastpage}";
$this->TagValues["lastpagelink"]="{lastpage}";
$this->TagValues["listpage"]="{list}";
$this->TagValues["listpagelink"]="{list}";
/*其他标签直接替换
{$datacount}:共{$datacount}条记录
{$currentpage}:当前为第{$currentpage}页
{$totalpage}:共{$totalpage}页
{$numperpage}:每页{$numperpage}条
*/
}
// 重新设定标签对应值
function SetTagValue($item,$itemvalue="",$value=""){
if(!isset($item)||!isset($itemvalue)||!isset($value)){return;}
foreach($this->TagItems as $key=>$v){
if($key==$item){
(emptyempty($itemvalue))?"":$this->TagItems[$key]=$itemvalue;//如果为空,则不改变
(emptyempty($value))?"":$this->TagValues[$key]=$value;
}
}
}

//模板解析
function PraseTemplate(){
//------A_begin------//
if($this->TotalPage > 1){
//------B_begin------//
if($this->CurrentPage > 1){
//首页
$t=str_replace("{link}",$this->PageUrl."1",$this->TagItems["firstpagelink"]);
$this->tpl=str_replace($this->TagValues["firstpagelink"],$t,$this->tpl);
//前一页
$t=str_replace("{link}",$this->PageUrl.($this->CurrentPage-1),$this->TagItems["previouspagelink"]);
$this->tpl=str_replace($this->TagValues["previouspagelink"],$t,$this->tpl);
//------C_begin------//
if($this->CurrentPage < $this->TotalPage){
//下一页
$t=str_replace("{link}",$this->PageUrl.($this->CurrentPage+1),$this->TagItems["nextpagelink"]);
$this->tpl=str_replace($this->TagValues["nextpagelink"],$t,$this->tpl);
//尾页
$t=str_replace("{link}",$this->PageUrl.$this->TotalPage,$this->TagItems["lastpagelink"]);
$this->tpl=str_replace($this->TagValues["lastpagelink"],$t,$this->tpl);
}else{
//下一页
$this->tpl=str_replace($this->TagValues["nextpage"],$this->TagItems["nextpage"],$this->tpl);
//尾页
$this->tpl=str_replace($this->TagValues["lastpage"],$this->TagItems["lastpage"],$this->tpl);
}
//------C_end------//
}else{
//首页
$this->tpl=str_replace($this->TagValues["firstpage"],$this->TagItems["firstpage"],$this->tpl);
//前一页
$this->tpl=str_replace($this->TagValues["previouspage"],$this->TagItems["previouspage"],$this->tpl);
//下一页
$t=str_replace("{link}",$this->PageUrl.($this->CurrentPage+1),$this->TagItems["nextpagelink"]);
$this->tpl=str_replace($this->TagValues["nextpagelink"],$t,$this->tpl);
//尾页
$t=str_replace("{link}",$this->PageUrl.$this->TotalPage,$this->TagItems["lastpagelink"]);
$this->tpl=str_replace($this->TagValues["lastpagelink"],$t,$this->tpl);
}
//------B_end------//
}else{
//解析前一页,前十页,后一页,后十页,首页,尾页
$this->tpl=str_replace($this->TagValues["previouspage"],$this->TagItems["previouspage"],$this->tpl);
$this->tpl=str_replace($this->TagValues["previoustenpage"],$this->TagItems["previoustenpage"],$this->tpl);
$this->tpl=str_replace($this->TagValues["nextpage"],$this->TagItems["nextpage"],$this->tpl);
$this->tpl=str_replace($this->TagValues["nexttenpage"],$this->TagItems["nexttenpage"],$this->tpl);
$this->tpl=str_replace($this->TagValues["firstpage"],$this->TagItems["firstpage"],$this->tpl);
$this->tpl=str_replace($this->TagValues["lastpage"],$this->TagItems["lastpage"],$this->tpl);
}
//------A_end------//

//前十页,后十页
if($this->CurrentPage-10>=1){
$t=str_replace("{link}",$this->PageUrl.($this->CurrentPage-10),$this->TagItems["previoustenpagelink"]);
$this->tpl=str_replace($this->TagValues["previoustenpagelink"],$t,$this->tpl);
}else{
$this->tpl=str_replace($this->TagValues["previoustenpage"],$this->TagItems["previoustenpage"],$this->tpl);
}
if($this->CurrentPage+10<=$this->TotalPage){
$t=str_replace("{link}",$this->PageUrl.($this->CurrentPage+10),$this->TagItems["nexttenpagelink"]);
$this->tpl=str_replace($this->TagValues["nexttenpagelink"],$t,$this->tpl);
}else{
$this->tpl=str_replace($this->TagValues["nexttenpage"],$this->TagItems["nexttenpage"],$this->tpl);
}

//数字列表
$firstnum;
$lastnum;
$t="";
if($this->CurrentPage-$this->LeftOffSet<1){
$firstnum=1;
}else{$firstnum=$this->CurrentPage-$this->LeftOffSet;}
if($this->CurrentPage+$this->RightOffSet>$this->TotalPage){
$lastnum=$this->TotalPage;
}else{$lastnum=$this->CurrentPage+$this->RightOffSet;}
for($i=$firstnum;$i<=$lastnum;$i++){
if($i==$this->CurrentPage){
$t.=str_replace("{list}",$i,$this->TagItems["listpage"]);
}else{
$m=str_replace("{list}",$i,$this->TagItems["listpagelink"]);
$t.=str_replace("{link}",$this->PageUrl.$i,$m);
}
}
$this->tpl=str_replace($this->TagValues["listpage"],$t,$this->tpl);
//$list=str_replace("{list}","1",$this->TagItems["listpage"]);
//$this->tpl=str_replace($this->TagValues["listpage"],$list,$this->tpl);

//共{$datacount}条记录,当前为第{$currentpage}页,共{$totalpage}页,每页{$numperpage}条
$this->tpl=str_replace("{datacount}",$this->RecordCount,$this->tpl);
$this->tpl=str_replace("{currentpage}",$this->CurrentPage,$this->tpl);
$this->tpl=str_replace("{totalpage}",$this->TotalPage,$this->tpl);
$this->tpl=str_replace("{numperpage}",$this-
>PageSize,$this->tpl);
}

//输出
function OutPut(){
return $this->tpl;
}
}

?>
2,自定义CSS样式文件:style.html

XML/HTML代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pagetpl_1</title>
<style type="text/css">
body,div,table,tr,td {font-family:arial;font-size:14px;}
.global {text-align:left;padding:2px;}
.index {
border-style:solid;
border-width:1px;
color:#0099CC;
height:15px;
text-align:center;
float:left;
margin:2px;
padding:4px;
}
.index a:link{text-decoration:none;color:#3399FF;font-weight:bold;}
</style>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<!--templatebegin-->
<div class="global">
<div class="index">{previoustenpage}</div>
<div class="index">{firstpage}</div>
<div class="index">{previouspage}</div>
<div class="index">{list}</div>
<div class="index">{nextpage}</div>
<div class="index">{lastpage}</div>
<div class="index">{nexttenpage}</div>

<div class="index">共{totalpage}页</div>
<div class="index">每页{numperpage}条</div>
<div class="index">当前为第{currentpage}页</div>
<div class="index">共{datacount}条记录</div>

</div>
<!--templateend-->
</body>
</html>

3,实例调用文件:demo.php,注意数据库连接。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pagetpl_1</title>
<style type="text/css">
body,div,table,tr,td {font-family:arial;font-size:14px;}
.global {text-align:left;padding:2px;}
.index {
border-style:solid;
border-width:1px;
color:#0099CC;
height:15px;
text-align:center;
float:left;
margin:2px;
padding:4px;
}
.index a:link{text-decoration:none;color:#3399FF;font-weight:bold;}
</style>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
require_once("page.class.php");
$conn=mysql_connect("localhost","root","root");//连接数据库
mysql_select_db("pht");//打开数据库
$p=new Page($_GET["page"],2,2,7,"?page=");//初始化
$sql="select * from comments ".$p->SetLimit();//构造select * from tb limit m,n语句
$p->ExecuteSql($sql,$conn);//执行sql
while($rs=$p->RecordSet()){//读出记录
echo "<br />".$rs["cname"]."<br />";
}
$sql="select count(cid) as uid from comments";//读出总记录数
$p->GetRecordCount($sql,$conn);
$p->GetTemplate("style.html");//获取模板内容
$p->PraseTemplate();//解析模板
Echo $p->OutPut();//输出分页
?>
</body>
</html>

标签:

给我留言