PHP:分页类

<?php
/* * *********************************************
 * @类名:   Page
 * @参数:   $page_total - 总记录数
 *          $page_size - 一页显示的记录数
 *          $page - 当前页
 *          $page_url - 获取当前的url
 * @功能:   分页实现
 */
class Page {
    private $page_total;          //总记录数
    private $page_size;           //一页显示的记录数
    private $page;           //当前页
    private $page_count;     //总页数
    private $page_i;              //起头页数
    private $page_en;             //结尾页数
    private $page_url;            //获取当前的url
    /*
     * $show_pages
     * 页面显示的格式,显示链接的页数为2*$show_pages+1。
     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     */
    private $show_pages;
    public function __construct($page_total = 1, $page_size = 1, $page = 1, $page_url, $show_pages = 2) {
        $this->page_total = $this->numeric($page_total);
        $this->page_size = $this->numeric($page_size);
        $this->page = $this->numeric($page);
        $this->page_count = ceil($this->page_total / $this->page_size);
        $this->page_url = $page_url;
        if ($this->page_total < 0)
            $this->page_total = 0;
        if ($this->page < 1)
            $this->page = 1;
        if ($this->page_count < 1)
            $this->page_count = 1;
        if ($this->page > $this->page_count)
            $this->page = $this->page_count;
        $this->limit = ($this->page - 1) * $this->page_size;
        $this->page_i = $this->page - $show_pages;
        $this->page_en = $this->page + $show_pages;
        if ($this->page_i < 1) {
            $this->page_en = $this->page_en + (1 - $this->page_i);
            $this->page_i = 1;
        }
        if ($this->page_en > $this->page_count) {
            $this->page_i = $this->page_i - ($this->page_en - $this->page_count);
            $this->page_en = $this->page_count;
        }
        if ($this->page_i < 1)
            $this->page_i = 1;
    }
    //检测是否为数字
    private function numeric($num) {
        if (strlen($num)) {
            if (!preg_match("/^[0-9]+$/", $num)) {
                $num = 1;
            } else {
                $num = substr($num, 0, 11);
            }
        } else {
            $num = 1;
        }
        return $num;
    }
    //地址替换
    private function page_replace($page) {
        return str_replace("{page}", $page, $this->page_url);
    }
    //首页
    private function page_home() {
        if ($this->page != 1) {
            return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
        } else {
            return "<p>首页</p>";
        }
    }
    //上一页
    private function page_prev() {
        if ($this->page != 1) {
            return "<a href='" . $this->page_replace($this->page - 1) . "' title='上一页'>上一页</a>";
        } else {
            return "<p>上一页</p>";
        }
    }
    //下一页
    private function page_next() {
        if ($this->page != $this->page_count) {
            return "<a href='" . $this->page_replace($this->page + 1) . "' title='下一页'>下一页</a>";
        } else {
            return"<p>下一页</p>";
        }
    }
    //尾页
    private function page_last() {
        if ($this->page != $this->page_count) {
            return "<a href='" . $this->page_replace($this->page_count) . "' title='尾页'>尾页</a>";
        } else {
            return "<p>尾页</p>";
        }
    }
    //输出
    public function page_write($id = 'page') {
        $str = "<div id=" . $id . ">";
        $str.=$this->page_home();
        $str.=$this->page_prev();
        if ($this->page_i > 1) {
            $str.="<p class='pageEllipsis'>...</p>";
        }
        for ($i = $this->page_i; $i <= $this->page_en; $i++) {
            if ($i == $this->page) {
                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
            } else {
                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
            }
        }
        if ($this->page_en < $this->page_count) {
            $str.="<p class='pageEllipsis'>...</p>";
        }
        $str.=$this->page_next();
        $str.=$this->page_last();
        $str.="<p class='pageRemark'>共<b>" . $this->page_count .
            "</b>页<b>" . $this->page_total . "</b>条数据</p>";
        $str.="</div>";
        return $str;
    }
}
?>

 来源:网络整理

posted @ 2020-07-16 15:46  wish_yang  阅读(202)  评论(0编辑  收藏  举报