php分页类代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

php分页类代码

/*

* PHP分页类

* @package Page

* @Created 2013-03-27

* @Modify 2013-03-27

* @link

* Example:

$myPage=new Pager(1300,intval($CurrentPage));

$pageStr= $myPage->GetPagerContent();

echo $pageStr;

*/

class Pager {

private $pageSize = 10;

private $pageIndex;

private $totalNum;

private $totalPagesCount;

private $pageUrl;

private static $_instance;

public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 10,$p_initNum=3,$p_initMaxNum=5) {

if (! isset ( $p_totalNum ) || !isset($p_pageIndex)) {

die ( "pager initial error" );

}

$this->totalNum = $p_totalNum;

$this->pageIndex = $p_pageIndex;

$this->pageSize = $p_pageSize;

$this->initNum=$p_initNum;

$this->initMaxNum=$p_initMaxNum;

$this->totalPagesCount= ceil($p_totalNum / $p_pageSize);

$this->pageUrl=$this->_getPageUrl();

$this->_initPagerLegal();

}

/**

* 获取去除page部分的当前URL字符串

*

* @return String URL字符串

*/

private function _getPageUrl() {

$CurrentUrl = $_SERVER["REQUEST_URI"];

$arrUrl = parse_url($CurrentUrl);

$urlQuery = $arrUrl["query"];

if($urlQuery){

$urlQuery = ereg_replace("(^|&)page=" . $this->pageIndex, "", $urlQuery);

$CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl);

if($urlQuery){

$CurrentUrl.="&page";

}

else $CurrentUrl.="page";

} else {

$CurrentUrl.="?page";

}

return $CurrentUrl;

}

/*

*设置页面参数合法性

*@return void

*/

private function _initPagerLegal()

{

if((!is_numeric($this->pageIndex)) || $this->pageIndex<1)

{

$this->pageIndex=1;

}elseif($this->pageIndex > $this->totalPagesCount)

{

$this->pageIndex=$this->totalPagesCount;

}

}

//$this->pageUrl}={$i}

//{$this->CurrentUrl}={$this->TotalPages}

public function GetPagerContent() {

$str = "

";

//首页上一页

if($this->pageIndex==1)

{

$str .="首页 "."\n";

$str .="上一页 "."\n"."\n";

}else

{

$str .="首页 "."\n";

$str .="上一页 "."\n"."\n";

}

/*

除首末后页面分页逻辑

*/

//10页(含)以下

$currnt="";