«

分享一个php分页类

时间:2024-2-17 15:32     作者:韩俊     分类: PHP


分享一个php分页类,自己写的,代码如下:

<?php
/**
 * Created by PhpStorm.
 * User: yhm
 * Date: 2018/1/7
 * Time: 20:24
 * Desc: 分页类
 * use: (new Page(200))->show();
 */

final class Page {

    const PAGE_SIZE = 20; //默认每页数据量

    private $_count; //数据总量
    private $_pageSize; //每页数据量
    private $_pages;
    private $_curPage;
    private $_curUrl;
    private $_pageName;

    final public function __construct($count, $pagesize = self::PAGE_SIZE, $page = 'page') {
        $this->_count = $count;
        $this->_pageSize = $pagesize;
        $this->_pages = ceil($this->_count / $this->_pageSize);
        $this->_pageName = $page;
        $this->_curPage = isset($_REQUEST[$page]) ? abs(intval($_REQUEST[$page])) : 1;

        $this->_getCurrentUrl();
    }

    private function _getCurrentUrl() {
        $this->_curUrl = getCurrentUrl();

        $this->_curUrl .= (false === strpos($this->_curUrl, '?')) ? '?' : '&';
    }

    private function _getIndex() {
        if ($this->_curPage == 1) {
            return '首页';
        } else {
            return '<a href="' . $this->_curUrl . $this->_pageName . '=1">首页</a>';
        }
    }

    private function _getEnd() {
        if ($this->_curPage == $this->_pages) {
            return '末页';
        } else {
            return '<a href="' . $this->_curUrl . $this->_pageName . '=' . $this->_pages . '">末页</a>';
        }
    }

    private function _getLast() {
        if ($this->_curPage <= 1) {
            return '上一页';
        } else {
            return '<a href="' . $this->_curUrl . $this->_pageName . '=' . ($this->_curPage - 1) . '">上一页</a>';
        }
    }

    private function _getNext() {
        if ($this->_curPage >= $this->_pages) {
            return '下一页';
        } else {
            return '<a href="' . $this->_curUrl . $this->_pageName . '=' . ($this->_curPage + 1) . '">下一页</a>';
        }
    }

    private function _getGoto() {
        $str = '<input id="pageInput" value="' . $this->_curPage . '" type="text" /><input onclick="javascript:window.location.href=\'';
        $str .= $this->_curUrl . $this->_pageName . '=\'+document.getElementById(\'pageInput\').value;" type="button" value="转到" />';
        return $str;
    }

    private function _style() {
        $str = '<style>';
        $str .= '#pageBar{text-align:right;}';
        $str .= '#pageBar #pageInput{width:40px;text-align:center;margin-right:3px;}';
        $str .= '#pageBar span{margin:3px 5px;padding:0;}';
        $str .= '</style>';

        return $str;
    }

    final public function show() {
        $limitStart = ($this->_curPage - 1) * $this->_pageSize;

        if ($this->_curPage < $this->_pages) {
            $limitEnd = $this->_curPage * $this->_pageSize;
        } else {
            $limitEnd = $this->_count;
        }

        echo $this->_style();

        echo sprintf(
            '<div id="pageBar"><span>共有数据 %d 条</span><span>每页显示 %d 条</span><span>当前 %d - %d 条</span>',
            $this->_count,
            $this->_pageSize,
            $limitStart,
            $limitEnd
        );

        if ($this->_count <= $this->_pageSize) {
            echo '</div>';
            return;
        }

        $indexLast = '<span>' . $this->_getIndex() . '</span><span>' . $this->_getLast() . '</span>';
        $pageStr = '';
        $endNext = '<span>' . $this->_getNext() . '</span><span>' . $this->_getEnd() . '</span>';
        $goto = '<span>' . $this->_getGoto() . '</span>';

        echo sprintf(
            '%s%s%s%s</div>',
            $indexLast,
            $pageStr,
            $endNext,
            $goto
        );
    }
}

该类会调用一个函数:

/**
 * 获取当前请求的网址
 * @return string
 */
function getCurrentUrl() {
    $url = $_SERVER["REQUEST_URI"];
    $_par = parse_url($url);

    if (isset($_par['query'])) {
        parse_str($_par['query'], $_query);
        $url = $_par['path'] . '?' . http_build_query($_query);
    }

    return $url;
}

标签: php php教程

热门推荐