又写了个分页类,不错,有长进
时间:2007-03-30 来源:lib
?php
/**
* 分页类
*
* 属性为public类型以便可以灵活使用,
* 比如把实现$this->subPageSetStyle()的功能放在调用该类的页面中(不用extends)完成
* 在php4下只要把属性都改成 var $var, 所有函数的public都去掉, 就可以
*
* @author 流水孟春 [email protected]
* $Date 2007.3.30
*/
class Meng_Subpage {
/**
* 提供输出的信息
*
* @var String
*/
public $subPageString;
/**
* 总页数
*
* @var int
*/
public $totalPage;
/**
* 每页显示记录数
*
* @var int
*/
public $displayPageNo;
/**
* 上一页的页码
*
* @var int
*/
public $lastPage;
/**
* 上一页的页码
*
* @var int
*/
public $prePage;
/**
* 下一页的页码
*
* @var int
*/
public $nextPage;
/**
* 分页的url查询变量
*
* @var string
*/
public $subPageVar;
/**
* 分页页的uri
*
* @var string
*/
public $uri;
/**
* 查询的起始项
*
* @var int
*/
public $firstCount;
function Meng_Subpage() {
}
/**
* 分页赋值
*
* @param int $total
* @param int $displayPageNo
* @param string $subPageVar
* @param string $uri
*/
public function subPageSetVar($total, $displayPageNo=10, $subPageVar = 'page', $uri='', $setStyle = 1) {
$this->totalPage = $total;
$this->displayPageNo = $displayPageNo;
$this->subPageVar = $subPageVar;
/* 分页页码(要打开第n页) */
if(empty($_REQUEST[$this->subPageVar]) || $_REQUEST[$this->subPageVar] = 0) {
$this->page = 1;
} else {
$this->page = (int) $_REQUEST[$this->subPageVar];
}
/* uri赋值,为空则赋值为本页URI */
if(empty($uri)){
$this->uri = $_SERVER["REQUEST_URI"];
} else {
$this->uri = $uri;
}
/* URI分析 */
$parseUri = parse_url($this->uri);
$uriQuery = $parseUri["query"]; // 单独取出URI的查询字串
/* 如果URI查询串非空 */
if (!empty($uriQuery)) {
/* 去掉URI可能已经有的页码信息 */
$uriQuery = ereg_replace("(&){0,1}" . $this->subPageVar . "=[0-9]{0,}", "", $uriQuery);
/* 将处理后的URI的查询串替换原来的URI的查询串 */
$this->uri = str_replace($parseUri["query"], $uriQuery, $this->uri);
/* 在URI后加"分页变量"查询信息,但待赋值 */
/* 如果去掉URI查询串后URI查询串变空 */
if(empty($uriQuery)) {
$this->uri = str_repeat('?', '', $this->uri);
$this->uri .= "?$this->subPageVar";
}
else {
$this->uri .= "&$this->subPageVar";
}
} else {
/* 如果URI查询串为空,直接加上"?分页变量"查询信息,但待赋值 */
$this->uri .= "?$this->subPageVar";
}
/* 页码计算 */
$this->lastPage = ceil($this->totalPage / $this->displayPageNo); // 最后页,也是总页数
$this->page = min($this->lastPage, $this->page); // page值超过最大值时取最大值做page值
$this->prePage = $this->page - 1; // 上一页
$this->nextPage = ($this->page == $this->lastPage) ? 0 : $this->page + 1; // 下一页
$this->firstCount = ($this->page - 1) * $this->displayPageNo; // 查询的起始项
if ($setStyle) {
$this->subPageSetStyle();
}
}
/**
* 分页导航条输出模板/样式
*
* @return bool
*/
function subPageSetStyle(){
/* 开始分页导航条代码 */
$this->subPageString = "显示第 " . ($this->totalPage ? ($this->firstCount + 1) : 0)
. "-" .min(($this->firstCount + $this->displayPageNo), $this->totalPage)
." 条记录,共 $this->totalPage 条记录
";
/* 如果只有一页则跳出方法 */
if($this->lastPage = 1) return false;
/* 首页 */
$this->subPageString .= " uri=1'>首页 ";
/* 前页 */
if($this->prePage) {
$this->subPageString.=" uri=$this->prePage'>前页 ";
} else {
$this->subPageString.=" 前页 ";
}
/* 后页 */
if($this->nextPage) {
$this->subPageString .= " uri=$this->nextPage'>后页";
} else {
$this->subPageString.=" 后页 ";
}
/* 尾页 */
$this->subPageString .= " uri=$this->lastPage'> 尾页 ";
/* 下拉跳转列表,循环列出所有页码 */
$this->subPageString .= " 到第 uri=\"+this.value'>\n";
for($i=1; $i=$this->lastPage; $i++){
if($i==$this->page) {
$this->subPageString .= "$i\n";
} else {
$this->subPageString .= "$i\n";
}
}
$this->subPageString .= " 页,共 $this->lastPage 页";
return true;
}
}
// 使用例子
$p = new Meng_Subpage();
$p -> subPageSetVar(100,4);
/*
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
//(列表内容略)
}
*/
// 输出分页导航条代码:
print $p->subPageString;
相关阅读 更多 +
排行榜 更多 +