+ -
当前位置:首页 → 问答吧 → 分页类 关于单列模式

分页类 关于单列模式

时间: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;
    }
}
?>


这里用到的是单例模式吗? 大家分析一下如果是单例模式,这里为什么这样用,有什么好处?

作者: kingarde   发布时间: 2010-08-23

作者: kingarde   发布时间: 2010-08-23