分页类 关于单列模式
时间:2010-08-23
来源:互联网
<?php
/**
* @package libs
* @id Page.class.php <仿迅雷看看分页类 2010-01-23>
* @author darkroad <http://www.tpblog.cn>
* Usage
* require_once('./libs/Page.class.php');
* $page = Page::getInstance($totalNum, $perPage);
* $pageNav = $page->makePageNav();
* echo $pageNav;
*/
class Page {
private $totalNum; // 总记录数
private $perPage; // 每页显示记录数
private $currPage; // 当前页
private $url; // 分页页面url
private $totalPages; // 总页数
private static $instance = null; // 分页类实例
/**
* 构造函数
* param int $totalNum 总记录数
* param int $perPage 每页显示记录数
* return null
*/
private function __construct($totalNum, $perPage) {
$this->totalNum = $totalNum;
$this->perPage = $perPage;
$this->url = self::getUrl();
$this->currPage = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
$this->totalPages = @ceil($this->totalNum / $this->perPage);
}
public static function getInstance($totalNum, $perPage) {
if(self::$instance == null) {
$c = __CLASS__;
self::$instance = new $c($totalNum, $perPage);
}
return self::$instance;
}
public function makePageNav() {
$pageStr = $numStr = '';
if($this->totalPage == 1) {
$pageStr = '';
}
else {
$pageStr .= $this->makeFirstAndPrePage();
if($this->totalPages <= 8) {
for($i=1;$i<=$this->totalPages;$i++) {
$numStr .= self::makePg($i, $this->currPage, $this->url);
}
}
else {
$pre4PageUrl = $next4PageUrl = '';
if ($this->currPage < 5) {
for($i=1;$i<=8;$i++) {
$numStr .= self::makePg($i, $this->currPage, $this->url);
}
}
else {
if($this->totalPages - $this->currPage >= 4) {
for($j=4;$j>=1;$j--) {
$a = $this->currPage-$j;
$b = $this->currPage+5-$j;
$pre4PageUrl .= "<a href='".str_replace("page={$this->currPage}", "page={$a}", $this->url)."'>{$a}</a>";
$next4PageUrl .= "<a href='".str_replace("page={$this->currPage}", "page={$b}", $this->url)."'>{$b}</a>";
}
$pageStr .= $pre4PageUrl."<strong>{$this->currPage}</strong>".$next4PageUrl;
}
else {
for($k=$this->totalPages-8;$k<=$this->totalPages;$k++) {
$numStr .= self::makePg($k, $this->currPage, $this->url);
}
}
}
}
$pageStr .= $numStr;
$pageStr .= $this->makeLastAndNextPage();
}
return $pageStr;
}
/**
* 生成首页和上一页链接
* return string
*/
private function makeFirstAndPrePage() {
$str = '';
if($this->currPage > 1) {
if($this->currPage > 2) {
$firstPageUrl = str_replace("page={$this->currPage}", "page=1", $this->url);
$str .= "<a href='{$firstPageUrl}'>« ...</a>";
}
$prePage = $this->currPage-1;
$prePageUrl = str_replace("page={$this->currPage}", "page={$prePage}", $this->url);
$str .= "<a href='{$prePageUrl}'>«上一页</a>";
}
return $str;
}
/**
* 生成下一页和尾页链接
* return string
*/
private function makeLastAndNextPage() {
$str = '';
if($this->currPage < $this->totalPages) {
$nextPage = $this->currPage+1;
$nextPageUrl = str_replace("page={$this->currPage}", "page={$nextPage}", $this->url);
$lastPageUrl = str_replace("page={$this->currPage}", "page={$this->totalPages}", $this->url);
$str .= "<a href='{$nextPageUrl}'>下一页»</a>";
if($this->currPage < $this->totalPages-1) {
$str .= "<a href='{$lastPageUrl}'>... »</a>";
}
}
return $str;
}
/**
* 生成数字页码链接
* return string
*/
private static function makePg($flagPg, $currPg, $url) {
$reStr = '';
if ($flagPg == $currPg) {
$reStr = '<strong>'.$flagPg.'</strong>';
}
else {
$reStr = "<a href='".str_replace("page={$currPg}", "page={$flagPg}", $url)."'>{$flagPg}</a>";
}
return $reStr;
}
private static function getUrl() {
$url = '';
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if (!ereg("(page=)", $url)) {
if (!strpos($url,"?")) {
$url = $url."?page=1";
}
else {
$url = $url."&page=1";
}
}
return $url;
}
}
?>
这里用到的是单例模式吗? 大家分析一下如果是单例模式,这里为什么这样用,有什么好处?
/**
* @package libs
* @id Page.class.php <仿迅雷看看分页类 2010-01-23>
* @author darkroad <http://www.tpblog.cn>
* Usage
* require_once('./libs/Page.class.php');
* $page = Page::getInstance($totalNum, $perPage);
* $pageNav = $page->makePageNav();
* echo $pageNav;
*/
class Page {
private $totalNum; // 总记录数
private $perPage; // 每页显示记录数
private $currPage; // 当前页
private $url; // 分页页面url
private $totalPages; // 总页数
private static $instance = null; // 分页类实例
/**
* 构造函数
* param int $totalNum 总记录数
* param int $perPage 每页显示记录数
* return null
*/
private function __construct($totalNum, $perPage) {
$this->totalNum = $totalNum;
$this->perPage = $perPage;
$this->url = self::getUrl();
$this->currPage = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
$this->totalPages = @ceil($this->totalNum / $this->perPage);
}
public static function getInstance($totalNum, $perPage) {
if(self::$instance == null) {
$c = __CLASS__;
self::$instance = new $c($totalNum, $perPage);
}
return self::$instance;
}
public function makePageNav() {
$pageStr = $numStr = '';
if($this->totalPage == 1) {
$pageStr = '';
}
else {
$pageStr .= $this->makeFirstAndPrePage();
if($this->totalPages <= 8) {
for($i=1;$i<=$this->totalPages;$i++) {
$numStr .= self::makePg($i, $this->currPage, $this->url);
}
}
else {
$pre4PageUrl = $next4PageUrl = '';
if ($this->currPage < 5) {
for($i=1;$i<=8;$i++) {
$numStr .= self::makePg($i, $this->currPage, $this->url);
}
}
else {
if($this->totalPages - $this->currPage >= 4) {
for($j=4;$j>=1;$j--) {
$a = $this->currPage-$j;
$b = $this->currPage+5-$j;
$pre4PageUrl .= "<a href='".str_replace("page={$this->currPage}", "page={$a}", $this->url)."'>{$a}</a>";
$next4PageUrl .= "<a href='".str_replace("page={$this->currPage}", "page={$b}", $this->url)."'>{$b}</a>";
}
$pageStr .= $pre4PageUrl."<strong>{$this->currPage}</strong>".$next4PageUrl;
}
else {
for($k=$this->totalPages-8;$k<=$this->totalPages;$k++) {
$numStr .= self::makePg($k, $this->currPage, $this->url);
}
}
}
}
$pageStr .= $numStr;
$pageStr .= $this->makeLastAndNextPage();
}
return $pageStr;
}
/**
* 生成首页和上一页链接
* return string
*/
private function makeFirstAndPrePage() {
$str = '';
if($this->currPage > 1) {
if($this->currPage > 2) {
$firstPageUrl = str_replace("page={$this->currPage}", "page=1", $this->url);
$str .= "<a href='{$firstPageUrl}'>« ...</a>";
}
$prePage = $this->currPage-1;
$prePageUrl = str_replace("page={$this->currPage}", "page={$prePage}", $this->url);
$str .= "<a href='{$prePageUrl}'>«上一页</a>";
}
return $str;
}
/**
* 生成下一页和尾页链接
* return string
*/
private function makeLastAndNextPage() {
$str = '';
if($this->currPage < $this->totalPages) {
$nextPage = $this->currPage+1;
$nextPageUrl = str_replace("page={$this->currPage}", "page={$nextPage}", $this->url);
$lastPageUrl = str_replace("page={$this->currPage}", "page={$this->totalPages}", $this->url);
$str .= "<a href='{$nextPageUrl}'>下一页»</a>";
if($this->currPage < $this->totalPages-1) {
$str .= "<a href='{$lastPageUrl}'>... »</a>";
}
}
return $str;
}
/**
* 生成数字页码链接
* return string
*/
private static function makePg($flagPg, $currPg, $url) {
$reStr = '';
if ($flagPg == $currPg) {
$reStr = '<strong>'.$flagPg.'</strong>';
}
else {
$reStr = "<a href='".str_replace("page={$currPg}", "page={$flagPg}", $url)."'>{$flagPg}</a>";
}
return $reStr;
}
private static function getUrl() {
$url = '';
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if (!ereg("(page=)", $url)) {
if (!strpos($url,"?")) {
$url = $url."?page=1";
}
else {
$url = $url."&page=1";
}
}
return $url;
}
}
?>
这里用到的是单例模式吗? 大家分析一下如果是单例模式,这里为什么这样用,有什么好处?
作者: kingarde 发布时间: 2010-08-23

作者: kingarde 发布时间: 2010-08-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28