php 分页

使用方法    

<?php
include 'page.class.php';
$object = new page(100,20);
$object->param=['title'=>'测试','name'=>'刘中胜'];
$data = $object -> setPage(); 
代码示例
<?php

class page
{
    //总数据条数
    private $count;
    //每页显示条数默认20条
    private $pageNum;
    //返回分页信息
    private $config = [];
    //设置首页按钮名字
    public $setHomeButtonName = '首页';
    //设置上一页显示名称
    public $setPreviousName = '上一页';
    //设置下一页显示名称
    public $setNextName = '下一页';
    //设置尾页显示名称
    public $setEndPageName = '尾页';
    public $style = 1;
    public $param = [];

    public function __construct($count = 0, $pageNum = 10)
    {
        $this->count = $count;
        $this->pageNum = $pageNum;
    }


    public function setPage()
    {
        //获取当前分页数
        $page = $this->getPage();
        //显示多少条分页
        $pageShowNum = 5;

        //获取总分页数
        $totalPage = $this->getTotalPage();

        //获取每页开始位置
        $pageShowStart = $this->getPageStart($page, $pageShowNum);

        //检测分页显示数是否大于分页总数
        if ($pageShowNum > $totalPage) {
            $pageShowNum = $totalPage;
        }

        //生成url
        $this->setHome($page);
        $this->setPrevious($page, $totalPage);
        $this->setNumPage($pageShowNum, $pageShowStart, $page);
        $this->setNext($page, $totalPage);
        $this->setEndPage($page, $totalPage);
        $this->config['total_page'] = $totalPage;
        $this->config['page_show_num'] = $pageShowNum;
        $this->config['page_show_start'] = $pageShowStart + 1;
        $this->config['page_show_end'] = $pageShowStart + $pageShowNum;
        $this->config['param'] = $this->param;
        return $this->config;
    }

    /**
     * 获取当前分页数
     **/
    private function getPage()
    {
        $page = @(int)$_GET['page'];
        if ($page == 0) {
            $page = 1;
        }
        return $page;
    }

    //获取总页数
    private function getTotalPage()
    {
        return ceil($this->count / $this->pageNum);
    }

    //获取每页开始位置
    private function getPageStart($page, $pageShowNum)
    {
        return (ceil($page / $pageShowNum) - 1) * $pageShowNum;
    }

    //生成首页按钮
    private function setHome($page)
    {
        if ($this->style == 1 && $page <= 1) {
            $this->config['home'] = "<a href='javascript:;' class='home disable'>" . $this->setHomeButtonName . "</a>";
        } else {
            $this->config['home'] = "<a href='?page=1" . $this->setParam() . "' class='home'>" . $this->setHomeButtonName . "</a>";
        }

        return $this->config['home'];
    }

    //生成上一页按钮
    private function setPrevious($page)
    {
        if ($this->style == 1 && $page <= 1) {
            $this->config['previous'] = "<a href='javascript:;' class='previous disable'>" . $this->setPreviousName . "</a>";
        } else {
            $this->config['previous'] = "<a href='?page=" . ($page - 1) . $this->setParam() . "' class='previous'>" . $this->setPreviousName . "</a>";
        }
        return $this->config['previous'];
    }

    //生成数字分页
    private function setNumPage($pageShowNum, $pageShowStart, $page)
    {
        $url = '';
        for ($i = 1; $i <= $pageShowNum; $i++) {
            $num = $pageShowStart + $i;
            if ($num == $page) {
                $url .= '<span style="color:red;">' . $num . '</span>';
            } else {
                $url .= '<a href="?page=' . $num . $this->setParam() . '" class="num">' . $num . '</a>';
            }
        }
        $this->config['num'] = $url;
        return $this->config['num'];
    }

    //生成下一页按钮
    private function setNext($page, $totalPage)
    {
        if ($this->style == 1 && $totalPage <= $page) {
            $this->config['next'] = "<a href='javascript:;' class='next disable'>" . $this->setNextName . "</a>";
        } else {
            $this->config['next'] = "<a href='?page=" . ($page + 1) . $this->setParam() . "' class='next'>" . $this->setNextName . "</a>";
        }
        return $this->config['next'];
    }

    //生成结束按钮
    private function setEndPage($page, $totalPage)
    {
        if ($this->style == 1 && $totalPage >= $page) {
            $this->config['end_page'] = "<a href='javascript:;' class='end_page disable'>" . $this->setEndPageName . "</a>";
        } else {
            $this->config['end_page'] = "<a href='?page=" . $totalPage . $this->setParam() . "' class='end_page'>" . $this->setEndPageName . "</a>";
        }
        return $this->config['end_page'];
    }

    //设置参数
    private function setParam()
    {
        $paramData = $this->param;
        if (!empty($paramData)) {
            $param = '';
            foreach ($paramData as $key => $value) {
                $param .= '&'.$key.'='.urlencode($value);
            }
        } else {
            $param = '';
        }
        return $param;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值