<?php
/**********************
* 通用MYSQL分页类(支持PATH_INFO地址)
* 作者: 多菜鸟
* 邮箱: kingerq AT msn DOT com
* 创建时间: 2006-04-17
* 最后修改: 2008-03-06
* 说明:如果你的PATH_INFO地址规则与本类给定的不同,请自行修改paramStr()函数中的对应行
* 实例:
$p = new Page( 60, 10 );
$p->trunStr = array( "First", "Prev", "Next", "End" );//设置分页显示字符
//输出分页字符串,也可以trunUp(),trunNum(),trunDown()和jumpTo()分开来输出显示
echo $p->printPage();
//echo $p->limitStr();//显示MYSQL中LIMIT部分字符串
//echo $p->pageCount();//总页数
**********************
*/
class Page {
var $perPage = 11; //每页显示的页码数
var $currentPage = 1; //当前页码
var $paramName = "pageNum"; //翻页参数名称
var $limitStr = ""; //MYSQL中LIMIT参数字符串
var $pageSize = 20; //每页记录数
var $trunStr = array( "首页", "上页", "下页", "末页" ); //翻页字符
/*
* 构造函数
* $recordCount 为记录总数量
* $pageSize 为每页显示记录数
*/
function Page( $recordCount = 0, $pageSize = 0 ) {
if( ! is_numeric( $recordCount ) || ! is_numeric( $pageSize ) ) return;
//记录总数
if( intval( $recordCount ) == 0 )
$recordCount = $this->recordCount;
else
$this->recordCount = $recordCount;
//每页显示记录数
if( intval( $pageSize ) == 0 )
$pageSize = $this->pageSize;
else
$this->pageSize = $pageSize;
$this->currentPage();//当前页码处理
//为当前页定界
if( $this->currentPage > $this->pageCount() )
$this->currentPage = $this->pageCount() < 1 ? 1 : $this->pageCount();
}
//格式化输出,可自定义
function printPage() {
$pageStr = $this->trunUp(); //向上
$pageStr .= $this->trunNum(); //数字页码
$pageStr .= $this->trunDown(); //向下
$pageStr .= $this->jumpTo(); //跳转
return $pageStr;
}
//向上翻处理
function trunUp() {
$pageStr = "";
if( $this->currentPage == 1 ) {
$pageStr .= $this->trunStr[0] . " " . $this->trunStr[1] . " ";
}else {
$pageStr .= "<a href=/"".$this->paramStr(1)."/" title=/"".$this->trunStr[0]."/">".$this->trunStr[0]."</a> ";
$pageStr .= "<a href=/"".$this->paramStr($this->currentPage - 1)."/" title=/"".$this->trunStr[1]."/">".$this->trunStr[1]."</a> ";
}
return $pageStr;
}
//每页显示的页码连接
function trunNum() {
$pageStr = "";
//中间页码值
$midNum = ceil($this->perPage/2);
//当前页开始显示的页码
$start = 0;
if( $this->currentPage - $midNum >= 0 ) {
if($this->pageCount() - $this->perPage > $this->currentPage - $midNum) {
$start = $this->currentPage - $midNum;
}else {
$start = $this->pageCount() - $this->perPage;
}
}
if( intval($start) < 0 ) $start = 0;
//当前页最后一个页码
$end = $this->perPage + $start >= $this->pageCount() ? $this->pageCount() : $this->perPage + $start;
//当前页全部页码
for( $i = $start + 1; $i <= $end; $i++ ) {
$pageStr .= $i != $this->currentPage
? " <a href=/"".$this->paramStr($i)."/" style='text-decoration:none;'>$i</a> "
: " <span style='text-decoration:underline;'>$i</span> ";
}
return $pageStr;
}
//向下翻处理
function trunDown() {
$pageStr = "";
if( $this->pageCount() == $this->currentPage || $this->pageCount() == 0 ) {
$pageStr .= $this->trunStr[2] . " " . $this->trunStr[3] . " ";
} else {
$pageStr .= "<a href=/"".$this->paramStr($this->currentPage + 1)."/" title=/"".$this->trunStr[2]."/">".$this->trunStr[2]."</a> ";
$pageStr .= "<a href=/"".$this->paramStr($this->pageCount())."/" title=/"".$this->trunStr[3]."/">".$this->trunStr[3]."</a> ";
}
return $pageStr;
}
//跳转
function jumpTo() {
$pageStr = "<select οnchange=/"location.href=this.options[this.selectedIndex].value/">/n";
for($i = 1; $i <= $this->pageCount(); $i++){
if($this->currentPage == $i) $selected = " selected";
else $selected = "";
$pageStr .= "<option value='".$this->paramStr($i)."'$selected>$i</option>/n";
}
$pageStr .= "</select>";
return $pageStr;
}
//当前页参数
function paramStr($num = 1) {
$paramStr = "";
unset( $_GET[$this->paramName] );//去掉翻页参数
if( $_SERVER["PATH_INFO"] ) {
//处理成PATH_INFO形如:/index.php/company-detail/id-122/pageNum-2/index.html
if( count( $_GET ) ) {
foreach( $_GET AS $param => $value ) {
$paramStr .= $paramStr ? "/" : "/index.php/";
$paramStr .= $param . "-" .urlencode($value);
}
$paramStr .= "/".$this->paramName."-".$num."/index.html";
} else {
$paramStr = "/".$this->paramName."-".$num."/index.html";
}
}else{
//处理成普通URL形如:?company=detail&id=122&pageNum=2
if( count( $_GET ) ) {
foreach( $_GET AS $param => $value ) {
$paramStr .= $paramStr ? "&" : "?";
$paramStr .= $param . "=" . urlencode($value);
}
$paramStr .= "&".$this->paramName."=".$num;
} else {
$paramStr = "?".$this->paramName."=".$num;
}
}
return $paramStr;
}
//取得当前页码
function currentPage() {
if( isset( $_GET[$this->paramName] ) && intval( $_GET[$this->paramName] ) > 0 ) {
$this->currentPage = intval( $_GET[$this->paramName] );
}
return $this->currentPage;
}
//总页数
function pageCount() {
return ceil( $this->recordCount / $this->pageSize );//总页数
}
//MYSQL中LIMIT部分字符串
function limitStr() {
return " LIMIT " . ( ( $this->currentPage - 1 ) * $this->pageSize ) . ", " . $this->pageSize;
}
//设置每页显示页码数,页码数至少大于1
function setPerPage($num = 5) {
if( intval($num) > 1 )
$this->perPage = intval($num);
}
}
?>