Home > php教程 > PHP源码 > 自己开发的分页类,可以适用于各种使用原生sql的场景

自己开发的分页类,可以适用于各种使用原生sql的场景

WBOY
Release: 2016-06-06 09:32:34
Original
1147 people have browsed it
跳至 [1] [2] [全屏预览]
<?php
/**
 * 分页类
*/
class Page
{
    /**
     * @var integer $current_page 当前页数
    */
    protected $current_page;
    /**
     * @var integer $totle_pages 总共的页数
    */
    protected $totle_pages;
    /**
     * @var integer $per_page_number 每页的数量
    */
    protected $per_page_number;
    /**
     * @var integer $totle_number 总共的数量
    */
    protected $totle_number;
    /**
     * 构造函数
     *
     * @param integer $totle_number 总共的记录数
     * @param integer $per_page_number 每页的记录数,默认为10
    */
    public function __construct($totle_number, $current_page, $per_page_number = 10){
        $this->totle_number = $totle_number;
        $this->per_page_number = $per_page_number;
        $this->totle_pages = $totle_number == 0 ? 1 :
        (($totle_number % $per_page_number == 0) ? floor($totle_number / $per_page_number) : floor($totle_number / $per_page_number) + 1);
        $this->current_page = $current_page > $this->totle_pages ? $this->totle_pages : $current_page <= 0 ? 1 : $current_page;
    }
    /**
     * 获取当前页数
     *
     * @return integer 当前页数
    */
    public function getCurrentPage(){
        return $this->current_page;
    }
    /**
     * 获取总共页数
     *
     * @return integer 总共页数
    */
    public function getTotlePages(){
        return $this->totle_pages;
    }
    /**
     * 获取当前查询应该从何处开始
     *
     * @return integer 当前查询应该开始的地方
    */
    public function getCurrentNum(){
        return ($this->current_page - 1) * $this->per_page_number;
    }
    /**
     * 获取每页数量
     *
     * @return integer 每页数量
    */
    public function getPerPageNum(){
        return $this->per_page_number;
    }
    /**
     * 获取前台显示的页数
     *
     * @param integer $max_show_page_number 最大显示的页数,默认为5
     * @return Array 包含了页数信息的数组
    */
    public function getPages($max_show_page_number = 5){
        if($max_show_page_number > $this->totle_pages){
            return $this->createArray(1, $this->totle_pages);
        }
        $half = floor($max_show_page_number / 2);
        if($this->current_page <= floor($this->totle_pages / 2)){
            if($this->current_page < $half){
                return $this->createArray(1, $max_show_page_number);
            }else{
                return $this->createArray($this->current_page - $half + 1, $this->current_page - $half + $max_show_page_number);
            }
        }else{
            if($this->totle_pages - $this->current_page < $half){
                return $this->createArray($this->totle_pages - $max_show_page_number, $this->totle_pages - 1);
            }else{
                return $this->createArray($this->current_page + $half - $max_show_page_number, $this->current_page + $half - 1);
            }
        }
    }
    /**
     * 创建一个数组,数组的值从$start 到 $end
     *
     * @param integer $start 数值开始的值
     * @param integer $end 数组结束的值
     * @param integer $step 数组的值增长的步长,默认为1
     * @return Array 如果步长为0返回空数组,如果步长大于0,返回从小到大排序的值,反之返回从大到小排序的值
    */
    private function createArray($start, $end, $step = 1){
        if($step == 0){
            return [];
        }
        $result = [];
        for($i = 0; $i <= ($end-$start); $i+= $step){
            $result[$i] = $start + $i;
        }
        return $result;
    }
}
?>
Copy after login

2. [代码][PHP]代码 跳至 [1] [2] [全屏预览]

<?php
require "pagination.php";
// 首先需要先获取数据总数
$count = $model->getCount(); // we suppose there is a model can do sql query
// 获取当前页数,此处是从get参数中获取
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
// 实例化page类
$page = new Page($count, $current_page);
// 查询当前页需要展示的信息,使用sql的LIMIT
$data = $model->query()->limit($page->getCurrentNum(), $page->getPerPageNum());
// 现在就可以使用一系列的get方法来获取你需要在页面上展示的信息了
// 把 $data , $current_page 和 $page->getPages() 传递给视图,想要怎么样展示就看你的意愿了。
// $page->getPages()可以自动获取当前页数附近的几页,返回一个数组,直接在前台展示即可。
?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template