PHP 分页类

Wesley13
• 阅读 604
//用法1:
echo Page::instance(32,10,5,5,'show?p=','&lang=zh')->show();

//用法2:
echo Page::instance()->show(32,10,5,5,'show?p=','&lang=zh');

//用法3:
$page = Page::instance();
$page->set_config('theme', ' %first% %up% %prev% %page% %next% %down% %last% ');
echo $page->show(32,10,5,5,'show?p=','&lang=zh');

//用法4:
$page = Page::instance(32,10,5,5,'show?p=','&lang=zh');
echo $page->get('page_theme');
echo $page->get('next');//获取下一页,这里显示有设置的主题

//用法5:
$page = Page::instance();
$page->set(32,10,5,5,'show?p=','&lang=zh');
echo $page->get('page_theme');
echo $page->get('next');//获取下一页,这里显示有设置的主题

//注:该类不能对已设数据进行覆盖,比如32,10这些在同一次的执行中不能被其他数据覆盖。如果要进行覆盖,可将已设数据更改为NULL





<?php



/*
 *
 * @ID:      分页类
 *
 * @auther:  欣儿
 *
 * @time:    2014/02/20
 *
 * @web:     http://my.oschina.net/xinger
 *
**/



class Page {

    public $left     = '%';
    public $right     = '%';
    public $first     = 1 ;
    
    public $count;
    public $perlogs;
    public $page;
    public $roll_page;
    public $url;
    public $suffix;
    
    public $total;//总页数
    public $prev;//上一页
    public $next;//下一页
    public $up;//上大翻页
    public $down;//下大翻页

    protected $now_page;
    protected $other_page;
    protected $prev_page;
    protected $next_page;
    protected $up_page;
    protected $down_page;
    protected $first_page;
    protected $last_page;
        
    protected $var = array();//暂存数据
    protected $config = array(
        'other_theme'=>'<span class="current"><a href="%page_url%">%i%</a></span>',//页面页
        'now_theme'=>'<span class="current_show"><a href="%now_url%">%i%</a></span>',//当前页 %page%为other和now的组合

        'prev_theme'=>'<span class="current_pre"><a href="%prev_url%" title="上一页"><上一页</a></span>',//url可在theme单独使用
        'next_theme'=>'<span class="current_next"><a href="%next_url%" title="下一页">下一页></a></span>',
        'up_theme'=>'<span class="current_up"><a href="%up_url%" title="<<"><<</a></span>',//
        'down_theme'=>'<span class="current_down"><a href="%down_url%" title=">>">>></a></span>',
        'first_theme'=>'<span class="current_start"><a href="%first_url%" title="首页">首页</a></span>',
        'last_theme'=>'<span class="current_end"><a href="%last_url%" title="尾页">尾页</a></span>',

        'prev_end_theme'=>'',//不显示的时候总是显示时显示的主题
        'next_end_theme'=>'',
        'up_end_theme'=>'',//
        'down_end_theme'=>'',
        'first_end_theme'=>'',
        'last_end_theme'=>'',
        
        'theme'=>' %first% %up% %prev% %page% %next% %down% %last% ',//显示样式 共有 %count% 条数据, 第 %now_page% / %total% 页 %prev_always% %up_always% %down_always% 
    );
    
    public function __construct($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
        $this->set($count, $perlogs, $now_page, $roll_page, $url, $suffix);
    }

    public static function instance($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
        static $var;
        if(isset($var)) return $var;
        
        $var = new self($count, $perlogs, $now_page, $roll_page, $url, $suffix);
        return $var;
    }
    //显示
    public function show($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {        
        $this->set($count, $perlogs, $now_page, $roll_page, $url, $suffix);
        $this->compile();
        
        $page = $this->var['page_theme'];    
        return $page;
    
    }
    //获取返回信息
    public function get($key = NULL) {
        $this->compile();
        if(NULL !== $key && isset($this->var[$key])) return $this->var[$key];
            return $this->var;
    }
    //设置: 条目总数, 每页显示条数目, 当前页码, 当前页码的前后页数, 页码的地址, 地址其他项
    public function set($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
        if (NULL === $this->count)         $this->count         = $this->var['count']         = $count;
        if (NULL === $this->perlogs)     $this->perlogs        = $this->var['perlogs']     = $perlogs;
        if (NULL === $this->page)         $this->page         = $this->var['now_page']     = $now_page;
        if (NULL === $this->roll_page)     $this->roll_page     = $this->var['roll_page']     = $roll_page;//与当前页左右相距的页数
        if (NULL === $this->url)         $this->url             = $this->var['url']         = $url;
        if (NULL === $this->suffix)     $this->suffix         = $this->var['suffix']         = $suffix;
    }

    public function set_config($name, $value = NULL) {
        if (isset($this->config[$name])) {
            $this->config[$name]    =   $value;
            return true;
        }
        return false;
    }
    
    public function get_config($name = NULL) {
        if (NULL === $name) return $this->config;
        
        if (isset($this->config[$name])) {
            return $this->config[$name];
        } 
        
        return false;
    }
    
    public function get_count() {
        return $this->count;
    }
    
    public function get_perlogs() {
        return $this->perlogs;
    }
    
    public function get_page() {
        return $this->page;
    }
    
    public function get_roll_page() {
        return $this->roll_page;
    }
    
    public function get_url() {
        return $this->url;
    }
    
    public function get_suffix() {
        return $this->suffix;
    }
    
    public function get_first() {
        return $this->first;
    }
    
    protected function get_total() {
        $this->total = @ceil($this->count / $this->perlogs);
        return $this->total;
    }
    
    protected function get_prev() {
        $this->prev = $this->page - 1;
        return $this->prev;
    }
    
    protected function get_next() {
        $this->next = $this->page + 1;
        return $this->next;
    }
    
    protected function get_up($i = 5) {
        $this->up = $this->page - $i;
        return $this->up;
    }
    
    protected function get_down($i = 5) {
        $this->down = $this->page + $i;
        return $this->down;
    }
    
    protected function get_prev_page($always = false) {
        if ($always) $this->prev += 1;
        $this->prev_page = $this->url . $this->prev . $this->suffix;
        return $this->prev_page;
    }
    
    protected function get_next_page($always = false) {
        if ($always) $this->next -= 1;
        $this->next_page = $this->url . $this->next . $this->suffix;
        return $this->next_page;
    }
    
    protected function get_up_page($always = false) {
        $roll_page = $this->get_roll_page();
        $now_page = $this->get_page();
        $first = $this->get_first();
        
        if ($always) {
            if ($now_page <= $roll_page * 2 && $now_page > $roll_page) {
                $this->up += $roll_page;
            } else {
                $this->up = $first;
            }
        }
        $this->up_page = $this->url . $this->up . $this->suffix;
        return $this->up_page;
    }
    
    protected function get_down_page($always = false) {
        $total = $this->get_total();
        $roll_page = $this->get_roll_page();
        $now_page = $this->get_page();

        if ($always) {
            $page = $total - $now_page;
            if ($page <= $roll_page * 2 && $page >= $roll_page) {
                $this->down -= $roll_page;
            } else {
                $this->down = $total;
            }
        }
        $this->down_page = $this->url . $this->down . $this->suffix;
        return $this->down_page;
    }
    
    protected function get_first_page() {
        $this->first_page = $this->url . $this->first . $this->suffix;
        return $this->first_page;
    }
    
    protected function get_last_page() {
        $this->last_page = $this->url . $this->last . $this->suffix;
        return $this->last_page;
    }
    
    protected function get_now_page($i = NULL) {
        $this->now_page = $this->url . $i . $this->suffix;
        return $this->now_page;
    }
    
    protected function get_other_page($i = NULL) {
        $this->other_page = $this->url . $i . $this->suffix;
        return $this->other_page;
    }
    
    protected function get_prev_theme($always = false) {
        $total = $this->get_total();
        $now_page = $this->get_page();
        $prev = $this->get_prev();
        $prev_page = ($always === true) ? $this->get_prev_page(true) : $this->get_prev_page();    
                        
        $prev_always = NULL;    
        
        if($prev > 0 && $now_page <= $total) {
            $prev_always = strtr($this->config['prev_theme'], array($this->left . 'prev_url' . $this->right => $prev_page));
        }
        
        if($prev <= 0 && $always) {
            if(!empty($this->config['prev_end_theme'])) {
                $prev_theme = $this->config['prev_end_theme'];
            } else {
                $prev_theme = $this->config['prev_theme'];
            }
            $prev_always = strtr($prev_theme, array($this->left . 'prev_url' . $this->right => $prev_page));
        }
                
        return $prev_always;
    }
    
    protected function get_next_theme($always = false) {
        $next = $this->get_next();
        $total = $this->get_total();
        $next_page = ($always === true) ? $this->get_next_page(true) : $this->get_next_page();    

        $next_always = NULL;    
        
        if($next < $total + 1){
            $next_always = strtr($this->config['next_theme'], array($this->left . 'next_url' . $this->right => $next_page));
        }
        
        if($next >= $total + 1 && $always){
            if(!empty($this->config['next_end_theme'])) {
                $next_theme = $this->config['next_end_theme'];
            } else {
                $next_theme = $this->config['next_theme'];
            }
            $next_always = strtr($next_theme, array($this->left . 'next_url' . $this->right => $next_page));
        }
        
        return $next_always;
    }
    
    protected function get_up_theme($always = false) {
        $total = $this->get_total();
        $roll_page = $this->get_roll_page();
        $now_page = $this->get_page();
        $roll_page = $roll_page * 2;
        $up = $this->get_up($roll_page);
        $up_page = ($always === true) ? $this->get_up_page(true) : $this->get_up_page();    
                        
        $up_always = NULL;    
        
        if($up > 0 && $now_page <= $total){
            $up_always = strtr($this->config['up_theme'], array($this->left . 'up_url' . $this->right => $up_page));
        }
        
        if($up <= 0 && $always){
            if(!empty($this->config['up_end_theme'])){
                $up_theme = $this->config['up_end_theme'];
            } else {
                $up_theme = $this->config['up_theme'];
            }
            $up_always = strtr($up_theme, array($this->left . 'up_url' . $this->right => $up_page));
        }
        
        return $up_always;
    }
    
    protected function get_down_theme($always = false) {
        $total = $this->get_total();
        $roll_page = $this->get_roll_page();
        $roll_page = $roll_page * 2;
        $down = $this->get_down($roll_page);
        $down_page = ($always === true) ? $this->get_down_page(true) : $this->get_down_page();    

        $down_always = NULL;    
        
        if($down < $total + 1){
            $down_always = strtr($this->config['down_theme'], array($this->left . 'down_url' . $this->right => $down_page));
        }
        
        if($down >= $total + 1 && $always){
            if(!empty($this->config['down_end_theme'])){
                $down_theme = $this->config['down_end_theme'];
            } else {
                $down_theme = $this->config['down_theme'];
            }
            $down_always = strtr($down_theme, array($this->left . 'down_url' . $this->right => $down_page));
        }
        
        return $down_always;
    }
    
    protected function get_page_theme($always = false) {
        $total = $this->get_total();
        $now_page = $this->get_page();
        $roll_page = $this->get_roll_page();
        
        $page = NULL;

        for ($i = $now_page - $roll_page;$i <= $now_page + $roll_page && $i <= $total; $i++){
            if ($i > 0){
                if ($i == $now_page) {
                    $page .= strtr($this->config['now_theme'], array($this->left . 'page_url' . $this->right => $this->get_now_page($i),$this->left . 'now_url' . $this->right => $this->get_other_page($i),$this->left . 'i' . $this->right => $i));
                } else {
                    $page .= strtr($this->config['other_theme'], array($this->left . 'page_url' . $this->right => $this->get_now_page($i),$this->left . 'now_url' . $this->right => $this->get_other_page($i),$this->left . 'i' . $this->right => $i));
                }
            } 
        }
        
        if ($total <= 1 && $always == false) $page = '';
                
        return $page;
    }
    
    protected function get_first_theme($always = false) {
        $total = $this->get_total();
        $now_page = $this->get_page();
        $roll_page = $this->get_roll_page();
        $first_page = $this->get_first_page();    
        
        $first_always = NULL;

        if ($now_page > $roll_page + 1 && $now_page <= $total) {
            $first_always = strtr($this->config['first_theme'], array($this->left . 'first_url' . $this->right => $first_page));
        }
    
        if ($now_page <= $roll_page + 1 && $always) {
            if(!empty($this->config['first_end_theme'])) {
                $first_theme = $this->config['first_end_theme'];
            } else {
                $first_theme = $this->config['first_theme'];
            }
            $first_always = strtr($first_theme, array($this->left . 'first_url' . $this->right => $first_page));
        }
    
        return $first_always;
    }
    
    protected function get_last_theme($always = false) {
        $now_page = $this->get_page();
        $total = $this->get_total();
        $roll_page = $this->get_roll_page();
        $last_page = $this->get_last_page();

        $last_always = NULL;

        if ($now_page + $roll_page < $total || $always) {
            $last_always = strtr($this->config['last_theme'], array($this->left . 'last_url' . $this->right => $last_page));
        }
    
        if ($now_page + $roll_page >= $total && $always) {
            if(!empty($this->config['last_end_theme'])){
                $last_theme = $this->config['last_end_theme'];
            } else {
                $last_theme = $this->config['last_theme'];
            }
            $last_always = strtr($last_theme, array($this->left . 'last_url' . $this->right => $last_page));
        }
    
        return $last_always;
    }
    //compile
    protected function compile() {        
        $count         = $this->get_count();
        $now_page     = $this->get_page();
        $roll_page     = $this->get_roll_page();
    
        $total         = $this->var['total']         = $this->last = $this->get_total();
        $prev_url     = $this->var['prev_url']     = $this->get_prev_page();    
        $next_url     = $this->var['next_url']     = $this->get_next_page();    
        $first_url     = $this->var['first_url']     = $this->get_first_page();    
        $last_url     = $this->var['last_url']     = $this->get_last_page();            
                
        $prev     = $this->var['prev']     = $this->get_prev_theme();    
        $next     = $this->var['next']     = $this->get_next_theme();    
        $page     = $this->var['page']     = $this->get_page_theme();
        $up     = $this->var['up']         = $this->get_up_theme();    
        $down     = $this->var['down']     = $this->get_down_theme();    
        $first     = $this->var['first']     = $this->get_first_theme();    
        $last     = $this->var['last']     = $this->get_last_theme();    
                
        $prev_always     = $this->var['prev_always']     = $this->get_prev_theme(true);    
        $next_always     = $this->var['next_always']     = $this->get_next_theme(true);    
        $page_always     = $this->var['page_always']     = $this->get_page_theme(true);
        $up_always         = $this->var['up_always']         = $this->get_up_theme(true);    
        $down_always     = $this->var['down_always']     = $this->get_down_theme(true);    
        $first_always     = $this->var['first_always']     = $this->get_first_theme(true);    
        $last_always     = $this->var['last_always']     = $this->get_last_theme(true);    
                
        $theme_parse = array(
            $this->left . 'first' . $this->right => $first,
            $this->left . 'up' . $this->right => $up,
            $this->left . 'prev' . $this->right => $prev,
            $this->left . 'page' . $this->right => $page,
            $this->left . 'next' . $this->right => $next,
            $this->left . 'down' . $this->right => $down,
            $this->left . 'last' . $this->right => $last,

            $this->left . 'first_always' . $this->right => $first_always,
            $this->left . 'up_always' . $this->right => $up_always,
            $this->left . 'prev_always' . $this->right => $prev_always,
            $this->left . 'page_always' . $this->right => $page_always,
            $this->left . 'next_always' . $this->right => $next_always,
            $this->left . 'down_always' . $this->right => $down_always,
            $this->left . 'last_always' . $this->right => $last_always,

            $this->left . 'total' . $this->right => $total,
            $this->left . 'count' . $this->right => $count,
            $this->left . 'now_page' . $this->right => $now_page,
            $this->left . 'roll_page' . $this->right => $roll_page,
            
            $this->left . 'prev_url' . $this->right => $prev_url,
            $this->left . 'next_url' . $this->right => $next_url,
            $this->left . 'first_url' . $this->right => $first_url,
            $this->left . 'last_url' . $this->right => $last_url,
        );
        
        $page_theme = $this->var['page_theme'] = strtr($this->config['theme'], $theme_parse);
        
        return $page_theme;
    }
}

?>
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
mysql ,show slave status详解
\想确认sql\_thread线程是否应用完了io\_thread接收到的了relaylog,看Master\_Log\_FileRelay\_Master\_Log\_File,Read\_Master\_Log\_PosExec\_Master\_Log\_PosMaster_Log_File:mysqlbin.0000
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这