自定义 php 简单分页功能

<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/5/26
 * Time: 15:14
 */


class page
{
    private $path;
    private $tmpl='';
    private $limitPage;
    private $selectIndex=10;//每页下拉条数
    private $pageTotal; //总页数
    private $total;
    private $limit;
    private $pageIndex=3;

    /**
     * LampPage constructor.
     * @param $total integer 数据总数
     * @param $startPage integer 当前页码
     * @param $limit integer 规定的每页显示条数
     * @param $count integer 当前数据量
     * @param null $path 页码按钮路径
     * @param $ShowPage integer 显示的页码
     */
    public function __construct($total, $startPage, $limit,$path=null,$ShowPage=5,$count=null)
    {
        $this->limitPage=$startPage>0?$startPage:1; //页码
        $this->pageIndex= $ShowPage;
        $this->pageTotal = ceil($total / $limit); //得到总页数
        $this->total = $total; //数据总数
        $this->limit = $limit; //每页显示条数

        #任意输入只要 页码 大于 当前页码 都等于最大页码
        if($this->limitPage > $this->pageTotal)
        {
            $this->limitPage = $this->pageTotal;
        }

        $this->getPath($path)->template();
    }
    public function getPage()
    {
        return $this->tmpl;
    }
    private function template(){

        if($this->total>0){

            $this->tmpl.='<div class="pageAll clearFloat">';
            $this->pageCss()
                ->pageOne()
                ->pagePrevious()
                ->pageIndex()
                ->pageNext()
                ->pageLast()
                ->pageSelect()
                ->pageScript();
            $this->tmpl.='</div>';
        }
    }
    private function getPath($path=null)
    {

        JRoute::_(JUri::getInstance()->getPath());
        $link=$path?$path:JUri::getInstance()->getPath();
        $link = preg_replace("/[0-9]{1,3}.html/", "", $link);

        $this->path = JiagleRoute::_($link);
        return $this;
    }
    private function pageOne()
    {
        $Page=$this->limitPage-1;
        if($Page>0){
            $link=$this->path;
        }else{
            $link="javascript:;";
        }
        $this->tmpl.= '<a class="pageOne page-btn f-left" href="'.$link.'">Page one</a>';
        return $this;
    }
    private function pagePrevious()
    {
        $Page=$this->limitPage-1;
        if($Page>0){
            $link=$this->path.$Page.'.html';
        }else{
            $link="javascript:;";
        }
        $this->tmpl.= '<a class="previous page-btn f-left" href="'.$link.'">Previous</a>';
        return $this;
    }
    private function pageIndex()
    {
        $this->tmpl.= '<div class="pageIndex f-left">';
        $this->tmpl.= '<ul>';
        $pages=['<li class="page-btn page-on">'.$this->limitPage.'</li>'];
        $maxPage=$this->pageIndex;


        for($left=$this->limitPage-1,$right = $this->limitPage+1 ;($left>=1 || $right<=$this->pageTotal)&&count($pages)<=($maxPage-1);){
            if($left >= 1){
                array_unshift($pages,'<li class="page-btn"><a  href="'.$this->path.$left.'.html">'.$left.'</a></li>');//放到数组的开头
                $left--;
            }
            if($right <= $this->pageTotal){
                array_push($pages,'<li class="page-btn"><a  href="'.$this->path.$right.'.html">'.$right.'</a></li>');//放到数组的开头
                $right++;
            }
        }

        $this->intervalIndex($pages);


        $this->tmpl.=implode($pages);
        $this->tmpl.= ' </ul>';
        $this->tmpl.= ' </div>';
        return $this;
    }
    private function pageNext()
    {
        $Page=$this->limitPage+1;
        if($Page && $Page<=$this->pageTotal){
            $link=$this->path.$Page.'.html';
        }else{
            $link="javascript:;";
        }
        $this->tmpl.= '<a class="pageNext page-btn f-left" href="'.$link.'">Next</a>';
        return $this;
    }
    private function pageLast()
    {
        if($this->pageTotal>$this->limitPage){
            $link=$this->path.$this->pageTotal.'.html';
        }else{
            $link="javascript:;";
        }
        $this->tmpl.= '<a class="pageLast page-btn f-left" href="'.$link.'">Last</a>';
        return $this;
    }
    private function intervalIndex(&$pages){
        $intervalIndex= ($this->pageIndex*2)+$this->limitPage;
         if($this->pageTotal > $intervalIndex){
             $href=$this->path.$intervalIndex.'.html';
             array_push($pages,'<li class="page-btn">...</li>','<li class="page-btn"><a  href="'.$href.'">'.$intervalIndex.'</a></li>');
         }else{
             $href=$this->path;
             $intervalIndex=1;
             array_unshift($pages,'<li class="page-btn"><a  href="'.$href.'">'.$intervalIndex.'</a></li>','<li class="page-btn">...</li>');
             if($this->pageTotal > ($this->limitPage+$this->pageIndex)){
                 $href=$this->path.$intervalIndex.'.html';
                 $intervalIndex=$this->pageTotal;
                 array_push($pages,'<li class="page-btn">...</li>','<li class="page-btn"><a  href="'.$href.'">'.$intervalIndex.'</a></li>');
             }
         }
    }
    private function pageSelect()
    {
        $this->tmpl.=' <div class="el-select-parent arrow-parent f-left page-btn">';
        $this->tmpl.='<input type="text" class="el-select-input el-select-placeholder" name="limit" readonly value="'.$this->selectIndex.'" />';
        $this->tmpl.='<el-select class="el-select">';

         for ($i=$this->selectIndex;$i<=50;$i=$i+5) {
             $this->tmpl .= '<el-option class="el-option" style="padding-left:0;"><a style=" display: inline-block;
    width: 100%;" href="'.$this->path.'?limit='.$i.'">'.$i.'</a></el-option>';
         }
        $this->tmpl .= '<el-option class="el-option" style="padding-left:0;"><a style=" display: inline-block;
    width: 100%;" href="'.$this->path.'?limit=50">50</a></el-option>';
        $this->tmpl .= '<el-option class="el-option" style="padding-left:0;"><a style=" display: inline-block;
    width: 100%;" href="'.$this->path.'?limit=100">100</a></el-option>';
        $this->tmpl.='</el-select>';
        $this->tmpl.='<div class="arrow"></div>';
        $this->tmpl.='</div>';
        return $this;
    }
   private function pageCss(){
 $this->tmpl.=<<<Eof
<style>
 .clearFloat:after{
    clear:both;content:'';display:block;width:0;height:0;visibility:hidden;
}
.f-left{
    float: left !important;
}
.f-right{
    float: right !important;
}
.pageAll{
    width: max-content;
    font-size: 14px;
        margin: 40px auto;
}
.pageAll .page-on,li.page-btn:hover,a.page-btn:hover{
    /*background: rgb(65, 87, 124);*/
    color: #ffb700;
  
}
.pageAll li a{
    width: 100%;
    height: 100%;
}
a.page-btn:hover a,li.page-btn:hover a{
 color: #ffb700;
}
.page-btn{
    box-sizing: border-box;
    border: 1px solid #e3e3e3;
    outline: none;
    box-shadow: none;
    background: #ffffff;
    color: #666666;
    cursor: pointer;
    display: inline-block;
    padding: 0 10px;
    margin: 0 5px;
    height: 30px;
    line-height: 30px;
    text-align: center;
}
.page-btn:last-child{
    margin-right: 0;
}
.pageIndex{
    margin-right: 5px;
}
li.page-btn{
    margin-right: 5px;
    padding: 0;
    min-width: 36px;
}

.el-select-input{
    cursor: default;
    display: block;
    height: 35px;
    width: 100%;
    line-height: 35px;
}
.el-select-input .country_img{
    margin-top: 12px !important;
}
.el-select-on{
    display: block !important;
}
.el-select-placeholder::placeholder{
    text-align: center;
    padding: 0;
}
.placeholder-span{
    color: rgb(117, 117, 117);
}
.el-select {
    display: none;
    position: absolute;
    z-index: 8;
    top: 40px;
    left: 0;
    text-align: left;
    /*display: block;*/
    height: auto;
    max-height: 300px;
    width: 95.5%;
    white-space: nowrap;
    overflow-y: auto;
    overflow-x: auto;
    background: #f5f5f5;;
    border: 1px #999999 solid;
    box-shadow: 0px 0px 2px 0px #999999;
}
/*.el-select::-webkit-scrollbar {*/
/*width: 0px;*/
/*}*/
/*.el-select::-webkit-scrollbar-thumb {*/
/*border-radius: 15px;*/
/*background: #cccccc;*/
/*}*/
/*.el-select::-webkit-scrollbar-track {*/
/*background: white;*/
/*}*/
.el-option{
    display: block;
    padding-left: 10px;
    line-height: 25px;
    cursor: default;
    color: #333333;
}
select:focus-visible{
    outline-color: #ffb700;
}
.el-option-on,.el-option-on a{
    background: rgb(30, 144, 255);
    color: #fff;
}
.arrow-parent{
    position: relative;
}
.arrow{
    width: 0;
    height: 0;
    border-top: 6px solid #666666;
    border-bottom: 6px solid transparent;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    position: absolute;
    right:3px;
    top: 10px;
}

.page-btn.el-select-parent{
    padding: 0;
    width: 70px;
    height: 30px;
}
.page-btn .el-select-input{
    height: 28px;
    width: 100%;
    padding: 0 16px;
}

   
</style>
Eof;
        return $this;
    }
    private function pageScript(){
        $this->tmpl.=<<<Eof
<script type="text/javascript">

$(document).ready(function () {

    $(document).on('click', '.el-select-input,.arrow', function (e) {
        var el_select_input_val = $(this).val()
        $(this).siblings('.el-select').find('.el-option').each(function (index, element) {

            if ($(this).data('value') == el_select_input_val || $(this).text().trim() == el_select_input_val || $(this).attr('selected')) {
                $(this).siblings().removeClass('el-option-on').removeAttr('selected');
                $(this).addClass('el-option-on').attr('selected', 'selected')
            } else {
                if (index == 0) {
                    $(this).siblings().removeClass('el-option-on').removeAttr('selected');
                    $(this).addClass('el-option-on')
                }
            }
        });

        var el_select_input = $(this).parent().find('.el-select-input')

        var width = el_select_input.innerWidth();
        var height = el_select_input.height();
        var thatOffset = el_select_input.offset();
        var parentOffset = $(this).parent().offset();
        var left = thatOffset.left - parentOffset.left;
        var top = (thatOffset.top - parentOffset.top) + height;


        var el_select = $(this).parent().find('.el-select')
        var eTop = thatOffset.top;
        var scrollTop = $(window).height() - (eTop - $(window).scrollTop()) - height;

        if (scrollTop < el_select.height() && $(window).height() > el_select.height()) {
            var thaOuter = $(this).outerHeight(true) - height
            var el_selectOuter = el_select.outerHeight(true) - el_select.height()
            top = -(el_select.outerHeight(true) - (thaOuter - el_selectOuter))
        }

        el_select.css({
            left: left,
            width: width,
            top: top
        });


        selectToggleClass(el_select)
        e.stopPropagation();

    });

    $(document).on('mouseenter', '.el-option', function () {//绑定鼠标进入事件
        // $(this).addClass('hover');
        $(this).addClass('el-option-on').siblings().removeClass('el-option-on');
    });
    $(document).on('mouseleave', '.el-option', function () {//绑定鼠标划出事件
        // $(this).removeClass('hover');
    });


    $(document).on('click', '.el-option', function (e) {
        var value = $(this).data('value') || $(this).attr('value') || '';
        var name = $(this).data('name') || $(this).text().trim();
        var func = $(this).parent().data('func') || '';
        var select_input = $(this).parent().parent().find('.el-select-input');
        var el_select = $(this).parent();
        if (select_input[0].localName == 'input') {
            select_input.val(name);
        } else {
            select_input.html($(this).html());
        }

        var toggle = select_input.data('toggle');
        $(toggle).val(value).trigger("change");

        selectToggleClass(el_select)

        if (func && typeof (eval(func)) == "function") {
            var that = $(this)
            eval(func + "(that,value,name);");
        }

        e.stopPropagation();
    });

    function selectToggleClass(el_select) {
        $('.el-select').each(function (index, val) {
            if (index != $('.el-select').index(el_select)) {
                $(this).removeClass('el-select-on')
            }
        })
        el_select.toggleClass('el-select-on');
    }

    $(document).bind("contextmenu", function (e) {
        $('.el-select').removeClass('el-select-on');

    });
    $(document).click(function (event) {
        $('.el-select').removeClass('el-select-on');
    });
    
});
</script>
Eof;
        return $this;
    }

}

使用:

echo (new page(200,1,15))->getPage()

实例:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值