可以自己定义样式的万能分页类

         最近在一个框架里看到一个不错的分页类,可以做到通过配置就可以做到设定分页效果!然后自己在这个基础上,改写了下,做了些整理!就可以很容易一直到任何项目中去。分享下!求指点!

         思路是这样的:通过配置文件将分页的各个元素拆分,然后分页类读取配置信息重组分页。这样就可以很容易的操作分页的样式和分页效果了!配置信息如:

<?php

return $conf = array(
                    'default' => array(
                                  'per_page'               => 10,
                                  'num_links_show'         => 5,
                                  'num_point_start_end'    => 5,
                                  'show_first'             => true,
                                  'show_prev'              => true,
                                  'show_next'              => true,
                                  'show_last'              => true,
                                  'show_goto'              => true,
                                  'show_info'              => false,
                                  'show_point'             => true,
                                  'show_empty_button'      => false,

                                  'first_text'             => '第一页',
                                  'prev_text'              => '上一页',
                                  'next_text'              => '下一页',
                                  'last_text'              => '最后一页',
                                  'point_text'             => '...',

                                  'page_algorithm'         => 'default',

                                  'full_tag_open'          => '<div class="scott">',
                                  'full_tag_close'         => '</div>',
                                  'num_tag_open'           => '',
                                  'num_tag_close'          => '',
                                  'link_tag_open'          => '<a href=":url">',
                                  'link_tag_close'         => '</a>',
                                  'link_tag_cur_open'      => '<span class="current">',
                                  'link_tag_cur_close'     => '</span>',
                                  'button_tag_open'        => '<a href=":url" style="font-weight:bold">',
                                  'button_tag_close'       => '</a>',
                                  'button_tag_empty_open'  => '<span class="disabled">',
                                  'button_tag_empty_close' => '</span>',
                                  'point_tag_open'         => '<span>',
                                  'point_tag_close'        => '</span>',
                                  'goto_tag_open'          => ' goto <input type="text" size="3" name="page" value="" /> ',
                                  'goto_tag_clase'         => '',
                                 ),
                   );

分页类的核心代码如下:

    /**
     * render pager
     *
     * @param int $pagenumber
     * @param int $pagecount
     * @param string $baseurl
     *
     * @return string
     */
    private function renderPager($pageNumber, $pageCount, $baseUrl = '?page=:page')
    {
        $baseUrl = urldecode($baseUrl);

        $pager = $this->pageConf['num_tag_open'];

        if ($this->pageConf['show_first']) {
            $pager .= $this->renderButton('first', $pageNumber, $pageCount, $baseUrl);
        }

        if ($this->pageConf['show_prev']) {
            $pager .= $this->renderButton('prev', $pageNumber, $pageCount, $baseUrl);
        }

        $startPoint = 1;
        $endPoint   = $this->pageConf['num_links_show'];
        $numLinks   = ceil($this->pageConf['num_links_show'] / 2) - 1;

        if ($pageNumber > $numLinks) {
            $startPoint = $pageNumber - $numLinks;
            $endPoint   = $pageNumber + $numLinks;
        }

        if ($endPoint > $pageCount) {
            $startPoint = $pageCount + 1 - $this->pageConf['num_links_show'];
            $endPoint   = $pageCount;
        }

        if ($startPoint < 1) {
            $startPoint = 1;
        }

        $currentButton = '';

        if ($this->pageConf['show_point']) {
            for($page = 1; $page < $startPoint; $page++) {
                $url = str_replace(':page', $page, $baseUrl);
                if ($page > $this->pageConf['num_point_start_end']) {
                    $currentButton .= $this->pageConf['point_tag_open'] . $this->pageConf['point_text'] . $this->pageConf['point_tag_close'];
                    break;
                }
                $currentButton .= str_replace(':url', $url, $this->pageConf['link_tag_open']) . $page . $this->pageConf['link_tag_close'];
            }
        }

        for ($page = $startPoint; $page <= $endPoint; $page++) {
            $url = str_replace(':page', $page, $baseUrl);
            if ($page == $pageNumber) {
                $currentButton .= $this->pageConf['link_tag_cur_open'] . $page . $this->pageConf['link_tag_cur_close'];
            } else {
                $currentButton .= str_replace(':url', $url, $this->pageConf['link_tag_open']) . $page . $this->pageConf['link_tag_close'];
            }
        }

        if ($this->pageConf['show_point']) {
            $page = $pageCount - $this->pageConf['num_point_start_end'];
            if ($page > $endPoint) {
                $currentButton .= $this->pageConf['point_tag_open'] . $this->pageConf['point_text'] . $this->pageConf['point_tag_close'];
            }

            for($page += 1; $page >= $endPoint && $page <= $pageCount; $page++) {
                if ($page == $endPoint) continue;
                $url = str_replace(':page', $page, $baseUrl);
                $currentButton .= str_replace(':url', $url, $this->pageConf['link_tag_open']) . $page . $this->pageConf['link_tag_close'];
            }
        }

        $pager .= $currentButton;

        if ($this->pageConf['show_next']) {
            $pager .= $this->renderButton('next', $pageNumber, $pageCount, $baseUrl);
        }

        if ($this->pageConf['show_last']) {
            $pager .= $this->renderButton('last', $pageNumber, $pageCount, $baseUrl);
        }

        $pager .= $this->pageConf['num_tag_close'];

        return $pager;
    }


用法:

<?php
include './PageConf.php';
include './Conf.php';
include './Pagination.php';

$pagination = new Pagination();
$page = !empty($_GET['page']) ? $_GET['page'] : 1;
$pagination->pageConfigHandle->addConfig($conf);
$pagination->init();
$pager = $pagination->pager($page, 1000, '?page=:page');

?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pagination</title>
    <link href="./css.css" rel="stylesheet" type="text/css">
</head>
<body>
    <h3>Pagination Demo</h3>
    <?php echo $pager; ?>
</body>
</html>

详细见附件!

http://pan.baidu.com/s/1dDqxiE1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值