相信分页的问题让很多PHP程序员曾经或者正在头疼不已。现在像大家推荐一款简单实用的PHP分页类:XPage。
这个类的使用方法很简单,只有短短的几行:
//这是构造函数
//function __construct($result = null, $pageSize = 0, $curPage = 0, $createUrlHandle = ''){}
/**
* $result MYSQL结果集
* $pageSize 每页的记录数
* $curPage 当前第几页,如果不指定页可以再实例化后用setPage手动指定。如果那个也不写,默认读取$_GET['page']的数值
* $createUrlHandle 生成指定页号链接的函数名。会传一个数字型的$page过去。如果不指定,默认返回修改$_GET['page']后的链接
**/
//页的大小
$pageSize = 10;
//执行SQL查询语句获得一个结果集
$result = $db->query($sql);
//实例化一个分页类
$p = new XPage($result, 10);
//计算所有信息,如总记录数、总页数等
$p->calc();
//返回当前页需要的结果集,当然是10条啦
$datas = $p->getPageRows();
#do something here
//显示分页按钮,有中文的“下一页”等按钮
$datas->display('cn');
$datas->display(); //没有“下一页”等按钮
类的源代码:
# ** **************************************************************
# Alan Thu Dec 18 17:08:50 CST 2008
# 分页类
# ** **************************************************************
/** **************************************************************
用法:
$result = $DB_Doc_Read->query($sql);
$p = new XPage($result, 10);$p->calc();
$datas = $p->getPageRows();
$datas->display('cn');
$datas->display();
# ** **************************************************************/
class XPage{
//页大小
var $pageSize = 5;
//总页数
var $pageCount = 1;
//当前页
var $curPage = -1;
//最小页
var $minPage = 1;
//最大页
var $maxPage = 1;
//总记录数
var $count = 0;
//当前页的第一行
var $first = 0;
//最多显示的链接数
var $linksCount = 10;
//生成链接函数
var $createUrlHandle = "xpageCreateUrl";
//数据库结果集
var $result = null;
//GET里的键
var $getKey = 'page';
//皮肤
var $faces = array(
'simple' => array(
'firstWord' => "|
'preWord' => "
'nextWord' => ">",
'lastWord' => '>|'
),
'cn' => array(
'firstWord' => "首页",
'preWord' => "上一页",
'nextWord' => "下一页",
'lastWord' => '末页'
),
);
//
/**
* 初始化函数,目前只适用于SQL类型
*
* @param array $options
*/
function __construct($result = null, $pageSize = 0, $curPage = 0, $createUrlHandle = ''){
if(is_resource ($result)){
$this->result = $result;
}else{
$this->count = (int)$result;
}
if($pageSize = (int)$pageSize){
$this->pageSize = $pageSize;
}
if($curPage = (int) $curPage){
$this->curPage = $curPage;
}
$this->setCreateUrlHandle($createUrlHandle);
}
/**
* 计算
*/
function calc(){
if($this->result){
$r = & $this->result;
$this->count = (int)mysql_num_rows($r);
}
#通过计算得到信息
$this->pageCount = ceil($this->count / $this->pageSize);
if(!$this->pageCount) $this->pageCount = 1;
$this->maxPage = $this->minPage + $this->pageCount - 1;
#如果当前页=0,则说明应该自动从GET里获取当前页
if($this->curPage <= 0){
$this->setPage($this->getPageFromGet($this->getKey));
}
$this->curPage = min($this->curPage, $this->maxPage);
$this->first = ($this->curPage - 1) * $this->pageSize;
}
/**
* 从GET里获取当前页
*
* @param string $key
* @return int
*/
function getPageFromGet($key = 'page'){
$page = (int)$_GET[$key];
return $page;
}
/**
* 设置当前页
*
* @param int $curPage
*/
function setPage($curPage){
$this->curPage = intval($curPage);
if(!$this->curPage) $this->curPage = 1;
}
/**
* 显示
*
*/
function display($faceType = 'simple', $options = array()){
#$allwaysshow = false, $showstatis = true, $return = false
$ShowButtonsOnNoPage = isset($options['ShowButtonsOnNoPage']) ? $options['ShowButtonsOnNoPage'] : true;
$showStatis = isset($options['showStatis']) ? $options['showStatis'] : true;
$return = isset($options['return']) ? $options['return'] : false;
$showButtons = isset($options['showButtons']) ? $options['showButtons'] : true;
$outstr = '';
#当不超过一页时不显示
if(!$allwaysshow and $this->pageCount <= 1){
return;
}
$faces = $this->faces[$faceType];
$outstr .= '
- ';
$start = min(max(1, $this->maxPage - $this->linksCount + 1), max(1, $this->curPage - floor($this->linksCount/2) + 1));
$end = min($this->maxPage, $start + $this->linksCount - 1);
if($showButtons and ($ShowButtonsOnNoPage or $this->curPage > 1)){
$outstr .= '
' .$faces['firstWord']. '';
$outstr .= '
' .$faces['preWord']. '';
}
for($i=$start; $i<=$end; $i++){
if($this->curPage == $i){
$outstr .= "
{$i}";
}else{
$outstr .= '
'.$i.'';
}
}
if($showButtons and ($ShowButtonsOnNoPage or $this->curPage > 1)){
$outstr .= '
' .$faces['nextWord']. '';
$outstr .= '
' .$faces['lastWord']. '';
}
if($showStatis){
$outstr .= '
第'.$this->curPage .'/'.$this->maxPage.'页, 共'.$this->count.'条';
}
$outstr .= "
";
if($return){
return $outstr;
}else{
echo $outstr;
return true;
}
}
/**
* 获取URL,这个可以被子类继承
*
* @param int $page
* @return str
*/
function createUrl($page){
if($this->createUrlHandle && function_exists($this->createUrlHandle)){
$handle = $this->createUrlHandle;
return $handle($page);
}else{
return "";
}
}
/**
* 设置生成链接的函数
* @param string $handle 函数名
*/
function setCreateUrlHandle($func){
if(function_exists($func)){
$this->createUrlHandle = $func;
}
return true;
}
/**
* 获取当前页的第一行的行号,从0计数
*/
function getFirstLineNoNo(){
if($this->pageCount == 0) return 0;
return ($this->curPage - 1) * $this->pageSize;
}
/**
* 获取最后一行的行号,从0计数
*/
function getLastLineNo(){
if($this->pageCount == 0 ) return 0;
return min($this->getFirstLineNo() + $this->pageSize, $this->count);
}
/**
* 获取数据库对应页的所有项
* @param string $type row type as mysql_fetch_row
* @return array
*/
function getPageRows($type = MYSQL_BOTH){
if($this->result){
$results = array();
@mysql_data_seek($this->result, $this->getFirstLineNoNo());
for($i=0; $ipageSize; $i++){
$row = mysql_fetch_array ($this->result, $type);
if(!$row){
break;
}
$results[] = $row;
}
return $results;
}else{
return array();
}
}
}
function xpageCreateUrl($page){
$url = $_SERVER['QUERY_STRING'];
if($url){
if(preg_match('|page=\d*|i', $url)){
$url = preg_replace('|page=\d*|i', "page=$page", $url);
}else{
$url .= "&page=$page";
}
}else{
$url = "page=$page";
}
$url = $_SERVER['PHP_SELF'] . "?" . $url;
return $url;
}