ECHO陈文
关注互联网技术开发与维护运营

PHP自定义带省略号的分页类

有时候框架提供的分页类,不能够满足我们的需求,这个时候就需要进行自定义分页类来实现分页了

<?php
/**
 * php分页类,支持动态静态url
 */

class Pager
{

    public $url;
    private $maxpageno;
    private $pageno;

    function __construct($total, $pagesize, $url, $maxpageno = 7)
    {
        $this->pageno = ceil($total / $pagesize);
        $this->url = $url;
        $this->maxpageno = $maxpageno < 7 ? 7 : $maxpageno;
    }

    //分页
    function render($curpage = 1, $curclass = 'active')
    {
        if ($pos = strrpos($this->url, '.')) {
            $this->suffix = substr($this->url, $pos);
            $this->url = substr($this->url, 0, $pos);
        }
        //上页下页
        $pre_url = $next_url = 'javascript:;';
        if ($curpage > 1) {
            $pre_url = $this->url . ($curpage - 1) . $this->suffix;
        }
        if ($curpage < $this->pageno) {
            $next_url = $this->url . ($curpage + 1) . $this->suffix;
        }

        //省略
        if ($this->pageno > $this->maxpageno) {
            $half = floor(($this->maxpageno - 4) / 2);
            $half_start = $curpage - $half + 1;
            if ($this->maxpageno % 2 !== 0) --$half_start;
            $half_end = $curpage + $half;
        }
        if (($this->pageno - $curpage) < ($this->maxpageno - 3)) {
            $half_start = $this->pageno - $this->maxpageno + 3;
            unset($half_end);
        }
        if ($curpage <= ($this->maxpageno - 3)) {
            $half_end = $this->maxpageno - 2;
            unset($half_start);
        }
        if ($curpage == 1) {
            $page = $this->getpage($pre_url, ' class="disabled"', '上一页');
        } else {
            $page = $this->getpage($pre_url, '', '上一页');
        }

        for ($i = 1; $i <= $this->pageno; $i++) {
            if (isset($half_start) && $i < $half_start && $i > 1) {
                if ($i == 2) $page .= $this->getpage('javascript:;', ' class="disabled"', '...');
                continue;
            }
            if (isset($half_end) && $i > $half_end && $i < $this->pageno) {
                if ($i == ($half_end + 1)) $page .= $this->getpage('javascript:;', ' class="disabled"', '...');
                continue;
            }

            if ($i == $curpage) {
                $in = " class='{$curclass}'";
                $url = 'javascript:;';
            } else {
                $in = '';
                $url = $this->url . $i . $this->suffix;
            }
            $page .= $this->getpage($url, $in, $i);
        }
        if ($curpage == $this->pageno) {
            $page .= $this->getpage($next_url, ' class="disabled"', '下一页');
        } else {
            $page .= $this->getpage($next_url, '', '下一页');
        }

        return sprintf(
            '<ul class="pagination">%s</ul>',
            $page
        );
    }


    private function getpage($url, $class, $i)
    {
        if ($url == 'javascript:;') {
            return "<li{$class}><span>{$i}</span></li>";
        } else {
            return "<li{$class}><a href='{$url}'>{$i}</a></li>";
        }
    }
}

$curpage = isset($_GET['page']) ? $_GET['page'] : 1;
$page = new Pager(35, 2, '?page=');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css.css" />
</head>
<body>
	<p>动态分页:</p>
	<div class="pagelist">
		<?=$page->pagelist($curpage)?>
	</div>
	<p>静态分页,看url:</p>
	<div class="pagelist">
		<?php
		$page->url = '/html/news/list_.html';
		echo $page->pagelist($curpage);
		?>
	</div>
</body>
</html>

CSS样式

.pagination{text-align: center; margin-top: 35px;}
.pagination a, .pagination span{padding: 3px 10px; background: #e8eaeb; display: inline-block; text-align: center; line-height: 25px; color: #818181; font-size: 14px}
.pagination a:hover, .pagination .fenye-on, .pagination li.active span{background: #0395D5 !important; color: #fff !important}
.pagination li{display: inline-block;margin: 0 5px;}
.pagination li.disabled span{cursor: no-drop; background: #f7f7f7; color: #ccc;}

图片

以上这篇PHP自定义带省略号的分页类就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持芦苇派。

赞(0) 打赏
未经允许不得转载:芦苇派 » PHP自定义带省略号的分页类
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏