Home > php教程 > PHP源码 > body text

php分页类程序员

WBOY
Release: 2016-06-08 17:30:14
Original
1008 people have browsed it
<script>ec(2);</script>

class Page {
    // public param
    public $pageName    = 'page';   // 默认 page 标签,即 filename.php?pageName=4 中的 pageName
    public $prevPage    = '     public $nextPage    = '>';      // 下一页
    public $prevBar     = '     public $nextBar     = '>>';     // 下一分页条
    // private param
    private $_totalNums = 1;        // 总记录数
    private $_barNum    = 10;       // 分页条显示分页个数
    private $_totalPage = 0;        // 总页数
    private $_nowPage   = 1;        // 当前页
    private $_perPage   = 10;       // 每页显示记录数
    private $_preUrl    = '';       // url 前缀
    private $_preNow    = 0;        // 当前页前显示分页个数 (范围应该是 0 至 $_barNum-1)
    private $_fromPage  = 0;        // 分页条起点
    private $_toPage    = 0;        // 分页条末端
    private $_offset    = 0;        // sql 查询记录偏移量
    // public functions -----------------------------------------------------------------
    // 构造函数 初始化分页变量
    // @param $nums int     总记录数
    // @param $per  int     每页显示记录数
    // @param $now  int     当前页数
    // @param $url  string  url前缀 默认为空
    public function __construct($nums, $per = '', $preNow = '', $barNum = '', $now = '', $url = '') {
        // 初始化
        $this->_set($nums, $per, $preNow, $barNum, $now, $url);
        $this->_totalPage   = ceil($this->_totalNums/$this->_perPage);
        $this->_offset      = ($this->_nowPage - 1) * $this->_perPage;
    }
    // 上一页
    public function getPrevPage() {
        // 当前页不是第一页
        if($this->_nowPage > 1) {
            return $this->_setLink($this->_setUrl($this->_nowPage - 1), $this->prevPage, 'prevpage');
        }
    }
    // 下一页
    public function getNextPage() {
        // 当前页小于总页数
        if($this->_nowPage _totalPage) {
            return $this->_setLink($this->_setUrl($this->_nowPage + 1), $this->nextPage, 'nextpage');
        }
    }
    // 第一页
    public function getFirstPage() {
        // 起点不是第一页
        if(($this->_nowPage - $this->_preNow) > 1) {
            return $this->_setLink($this->_setUrl(1), '1...', 'firstpage');
        }
    }
    // 最后一页
    public function getLastPage() {
        // 末端不大于总页数
        if(($this->_nowPage - $this->_preNow + $this->_barNum) _totalPage) {
            return $this->_setLink($this->_setUrl($this->_totalPage), '...' . $this->_totalPage, 'lastpage');
        }
    }
    // 上一分页条
    public function getPrevBar() {
        // 起点大于一个分页条数
        if(($this->_nowPage - $this->_preNow) > $this->_barNum) {
            return $this->_setLink($this->_setUrl(($this->_nowPage - $this->_preNow) - $this->_barNum), $this->prevBar, 'prevbar');
        }
    }
    // 下一分页条
    public function getNextBar() {
        // 末端不大于总页数
        if(($this->_nowPage - $this->_preNow + $this->_barNum) _totalPage) {
            return $this->_setLink($this->_setUrl($this->_fromPage + $this->_barNum), $this->nextBar, 'nextbar');
        }
    }
    // 分页条
    public function pageBar() {
        // 初始化分页条的始末端点
        $this->_toPage = $this->_nowPage + ($this->_barNum - $this->_preNow - 1);
        if($this->_toPage > $this->_totalPage) {
            $this->_preNow = $this->_barNum - ($this->_totalPage - $this->_nowPage + 1);
            $this->_toPage = $this->_totalPage;
        }
        if($this->_toPage _barNum) {
            $this->_toPage = $this->_barNum;
        }
        $this->_fromPage = $this->_nowPage - $this->_preNow;
        if($this->_fromPage             $this->_fromPage = 1;
        }
        // 初始化分页条
        $return = '';
        for($i = $this->_fromPage; $i _toPage; $i++) {
            if($i != $this->_nowPage) {
                $return .= $this->_setLink($this->_setUrl($i), $i, 'page');
            } else {
                $return .= '' . $i . '';
            }
        }
        return $return;
    }
    // 返回偏移量 用于 sql 查询
    public function getOffset() {
        return $this->_offset;
    }
    // 返回总记录数
    public function getTotalNums() {
        return '' . $this->_totalNums . '';
    }
    // 显示分页
    public function showPage() {
        return $this->getTotalNums() . $this->getFirstPage() . $this->getPrevBar() . $this->getPrevPage() . $this->pageBar() . $this->getNextPage() . $this->getNextBar() . $this->getLastPage();
    }
    // private functions ----------------------------------------------------------------
    //
    private function _set($nums, $per, $preNow, $barNum, $now, $url) {
        // 设置总记录数
        if($nums > 0) {
            $this->_totalNums = $nums;
        }
        // 设置每页显示记录数
        if($per > 0) {
            $this->_perPage = $per;
        }
        // 设置当前页前显示分页个数
        if($preNow > 0) {
            $this->_preNow = $preNow;
        }
        // 设置分页条链接个数
        if($barNum > 0) {
            $this->_barNum = $barNum;
        }
        // 设置当前页
        if(empty($now)) {
            // 自动获取
            if(isset($_GET[$this->pageName])) {
                $this->_nowPage = intval($_GET[$this->pageName]);
            }
        } else {
            // $now 已手动处理
            $this->_nowPage = intval($now); //
        }
        // 设置 url 前缀
        if(!empty($url)) {
            // $url 已手动处理
            $this->_preUrl = $url . (stristr($url, '?') ? '&' : '?') . $this->pageName . '=';
        } else {
            // 自动获取
            if(empty($_SERVER['QUERY_STRING'])) {
                // url 中不存在查询
                $this->_preUrl = $_SERVER['REQUEST_URI'] . '?' . $this->pageName . '=';
            } else {
                if(stristr($_SERVER['QUERY_STRING'], $this->pageName . '=')) {
                    // 查询中有 page=n(2,3...)
                    $this->_preUrl = str_replace($this->pageName . '=' . $this->_nowPage, '', $_SERVER['REQUEST_URI']);
                    $lastCharacter = $this->_preUrl[strlen($this->_preUrl) - 1];
                    if($lastCharacter == '?' || $lastCharacter == '&') {
                        // page=n(2,3...) 在原 url 的末尾
                        $this->_preUrl .= $this->pageName . '=';
                    } else {
                        // page=n(2,3...) 不在原 url 的末尾
                        $this->_preUrl .= '&' . $this->pageName . '=';
                    }
                } else {
                    // 查询中没有 page=n(2,3...)
                    $this->preUrl = $_SERVER['REQUEST_URI'] . '&' . $this->pageName . '=';
                }
            }
        }
    }
    // 设置链接地址
    private function _setUrl($pageNo) {
        return $this->_preUrl . $pageNo;
    }
    // 设置链接
    private function _setLink($url, $link, $style = '') {
        $style = 'class="' . $style . '"';
        return '' . $link . '';
    }
}
//

使用方法

$total = 1245;
//class  Page($nums, $per = '', $preNow = '', $barNum = '', $now = '', $url = '')
$page = new Page($total);
$pager = $page->showPage();
$smarty->assign('pager', $pager);
$smarty->display('index.tpl');

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!