php 分页函数

pagination.php

<?php
class Pagination {
    public $baseurl    = '';
    public $page       = 1;
    public $pagesize   = 10;
    public $rowsTotal  = 10;
    public $length     = 10;
    public $pageTotal  = 1;
    public $offset     = 0;
    public $begin      = 0;
    public $end        = 0;
    //public $class      = 'on';
    public $class      = 'cur';
    public $html=0;
    public $type ='php';
    public $ppage=0; //前几页生成静态
    public $htmlbaseurl='';
    function __construct($config=array()) {
        foreach($config as $k=>$v) {
            $this->{$k} = $v;
        }
        $this->init($this->page, $this->pagesize, $this->rowsTotal);
    }

    public function init($page=1, $pageSize=5, $total_rows=5) {
        $this->page = $page = $page < 1 ? 1: $page;

        //$count = $count > 300 ? 300:$count;//条目总数

        $this->pageTotal = $pageTotal = ceil($total_rows/$pageSize);//总页数

        $page = ($page > $pageTotal && $pageTotal > 0) ? $pageTotal: $page;

        $this->offset = ($page - 1) * $pageSize;
        //$this->end = $end   = $pageSize;
        $this->baseurl();
    }

    /**
     * 获得页码区间
     * Enter description here ...
     */
    private function limit() {
        $mid = intval($this->length / 2);
        $begin = $this->page > $this->pageTotal ? $this->pageTotal : $this->page;
        $begin -= 5;
        $this->begin = ($begin < 1) ? 1:$begin;

        $end = $this->page + $mid;
        $this->end = $end < $this->pageTotal ? $end:$this->pageTotal;
    }

    private function baseurl() {
        if(!$this->baseurl) {
            parse_str($_SERVER['QUERY_STRING'], $param);
            unset($param['p']);
            //var_dump($param);
            $this->baseurl = $_SERVER['PHP_SELF'].'?'.http_build_query($param);
        }
    }

    public function page() {//print_r($this);
        $this->limit();
        //print_r($this);
        if($this->begin == 1 && $this->end == 1) return '';
        if($this->page < 2) {
            $html = '';
        }
        else {
            $html = "<a href='".$this->calcPager($this->baseurl,1)."'>1</a> <a href='".$this->calcPager($this->baseurl,$this->page-1)."'>上一页</a> ";
        }
        for($i=$this->begin; $i<=$this->end; $i++) {
            if($this->page != $i) {
                $html .= "<a href='".$this->calcPager($this->baseurl,$i)."'>$i</a> ";
            }
            else {
                $html .= "<span class='{$this->class}'>$i</span> ";
            }
        }

        if($this->page < $this->pageTotal) {
            $html .= "<a href='".$this->calcPager($this->baseurl,$this->page+1)."'>下一页</a> <a href='".$this->calcPager($this->baseurl,$this->pageTotal)."'>{$this->pageTotal}</a>";
        }

        return $html;
    }



    private function calcPager($baseurl,$page){
        if($this->type=='html'||($page<=$this->ppage)){
            if($this->htmlbaseurl){
                $baseurl=$this->htmlbaseurl;
            }
            return "{$baseurl}_".($page).".html";
        }
        return "{$baseurl}&page=".$page."";

    }
}

?>

以下是使用方法

include 'pagination.php';
$config['page']    = $para['page']   = empty($_GET['page']) ? 1 : intval($_GET['page']);
$config['pagesize']   = $para['pagesize']  = 10;

//$para 为查询条件数组,还可以包含其他查询条件,如$para['searchkey'],$para['tm1'],$para['tm2']等。

$data = getDataFromDB($para); //查询数据库调取数据,得到数组$data=array('num'=>200,'info'=>array());num为总条数,info为每页的数据,获取此数据必须传参数$para['page'] 和$para['pagesize'];
$config['rowsTotal']  = $data['num'];
$config['class']      = 'cur';

$config['baseurl']    = '?param1='.$param1.'&param2='.$param2;  //如:$config['baseurl'] = '?searchkey='.$para['searchkey'].'&tm1='.$para['tm1'].'&tm2='.$para['tm2'];
$page = new pagination($config);
$pageshow = $page->page();

var_dump($pageshow);

var_dump($data['info']);

原文地址:https://www.cnblogs.com/kwishly/p/3396152.html