• 技术文章 >php教程 >PHP源码

    ThinkPHP抽出来的分页类

    PHP中文网PHP中文网2016-05-25 17:12:00原创485

    代码片段

    1. [图片] default_theme.jpg

    2. [图片] theme_1.jpg

    3. [文件] demo_ajax.php ~ 805B


    <?php
    require_once 'mysql.class.php';
    require_once 'page.class.php';
    require_once 'config.php';
    
    $mysql = new MySQL(DB_HOST_NAME,DB_USER_NAME,DB_PASS_WORD,DB_DATABASE_NAME);
    
    $pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
    $page_row = 5;//每页显示条数
    $count = $mysql->count ( "select id from ajax" );//总数
    
    $p = new Page ($count,$page_row,7);
    
    $p->AjaxType = true;
    $p->isJumpPage = true;
    //$p->function_name = 'yourself';
    $p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
    
    $sql = "select * from ajax limit ".($pagenum-1)*$page_row.",".$page_row;
    $result = $mysql->query ( $sql );
    
    $return_data['data'] = $result;
    $return_data['page'] = $p->show();
    
    echo json_encode($return_data);

    4. [文件] page.class.php ~ 7KB

    <?php
    // +----------------------------------------------------------------------
    // |分页类
    // +----------------------------------------------------------------------
    // | Author: justmepzy(justmepzy@gmail.com)
    // +----------------------------------------------------------------------
    class Page{
    	// 起始行数
        public $firstRow;
        // 列表每页显示行数
        public $listRows;
        // 分页总页面数
        protected $totalPages;
        // 总行数
        protected $totalRows;
        // 当前页数
        protected $nowPage;
    	// 分页的栏的总页数
        protected $coolPages;
    	// 分页栏每页显示的页数
        protected $rollPage;
    	//是否显示linkpage
    	protected $isShowLinkPage = FALSE;
    	//是否显示输入页数跳转
    	public $isJumpPage = FALSE;
    	//是否使用Ajax,默认不使用
    	public $AjaxType = FALSE;
    	//JavaScript中使用的获取数据方法名
    	public $function_name = 'ajax_page';
    	// 分页显示定制
        protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'末页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end% %jump%');
    	/**
         +----------------------------------------------------------
         * 
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         * @param array $totalRows  总的记录数
         * @param array $listRows  每页显示记录数
         * @param array $rollpage  是否显示linkpage条数
         +----------------------------------------------------------
         */
    	public function __construct($totalRows,$listRows,$rollpage){
    		$this->totalRows = $totalRows;
            $this->listRows = $listRows;
    		$this->rollPage = $rollpage;
    		$this->listRows = !empty($listRows)?$listRows:10;
    		$this->isShowLinkPage = empty($rollpage)?false:true;
            $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
    		$this->coolPages  = ceil($this->totalPages/$this->rollPage);
            $this->nowPage  = !empty($_REQUEST['page'])?$_REQUEST['page']:1;
            if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
                $this->nowPage = $this->totalPages;
            }
            $this->firstRow = $this->listRows*($this->nowPage-1);
    		
    	}
    	/**
         +----------------------------------------------------------
         * 设置显示样式
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
    	public function setConfig($name,$value) {
            if(isset($this->config[$name])) {
                $this->config[$name]    =   $value;
            }
        }
    	/**
         +----------------------------------------------------------
         * 分页显示输出
         +----------------------------------------------------------
         * @access public
         +----------------------------------------------------------
         */
        public function show() {
            if(0 == $this->totalRows) return '';
    		$p = 'page';
    		$nowCoolPage      = ceil($this->nowPage/$this->rollPage);
    		$request_url = $_SERVER['REQUEST_URI'];
    		
    		if(strpos($request_url,'?')){
    			$url  =  $request_url;
    		}else{
    			$url  =  $request_url.'?';
    		}
    		
            $parse = parse_url($url);
            if(isset($parse['query'])) {
                parse_str($parse['query'],$params);
                unset($params[$p]);
                $url   =  $parse['path'].'?'.http_build_query($params);
            }
            
            //上下翻页字符串
            $upRow   = $this->nowPage-1;
            $downRow = $this->nowPage+1;
            if ($upRow>0){         
    			if($this->AjaxType)
    				$upPage='<a href=javascript:'.$this->function_name.'('.$upRow.');>'.$this->config['prev'].'</a>';//ajax方式
    			else
    				$upPage='<a href=\''.$url.'&'.$p.'='.$upRow.'\'>'.$this->config['prev'].'</a>';
            }else{
                $upPage='';
            }
    
            if ($downRow <= $this->totalPages){
    			if($this->AjaxType)
    				$downPage='<a href=javascript:'.$this->function_name.'('.$downRow.');>'.$this->config['next'].'</a>';//ajax方式
    			else
    				$downPage='<a href=\''.$url.'&'.$p.'='.$downRow.'\'>'.$this->config['next'].'</a>';
            }else{
                $downPage='';
            }
            // << < > >>
            if($this->nowPage == 1){
                $theFirst = '';
                $prePage = '';
            }else{
                $preRow =  $this->nowPage-$this->rollPage;
    			if($this->AjaxType)
    				$theFirst = '<a href=javascript:'.$this->function_name.'(1);>'.$this->config['first'].'</a>';//ajax方式
    			else
                	$theFirst = '<a href=\''.$url.'&'.$p.'=1\' >'.$this->config['first'].'</a>';
            }
            if($this->nowPage == $this->totalPages){
                $nextPage = '';
                $theEnd='';
            }else{
                $nextRow = $this->nowPage+$this->rollPage;
                $theEndRow = $this->totalPages;
    			if($this->AjaxType)
    				$theEnd = '<a href=javascript:'.$this->function_name.'('.$theEndRow.');>'.$this->config['last'].'</a>';//ajax方式
    			else
                	$theEnd = '<a href=\''.$url.'&'.$p.'='.$theEndRow.'\' >'.$this->config['last'].'</a>';
            }
            // 1 2 3 4 5
    		if($this->isShowLinkPage){
    			$linkPage = '';
            	for($i=1;$i<=$this->rollPage;$i++){
                $page=($nowCoolPage-1)*$this->rollPage+$i;
                //保证linkpage保持当前页不是最后一个
                if($this->nowPage%$this->rollPage ==0 ){
                	$page = ($nowCoolPage-1)*$this->rollPage+$i+$this->rollPage-1;
                }
                if($page!=$this->nowPage){
                    if($page<=$this->totalPages){
    					if($this->AjaxType)
    						$linkPage .= '&nbsp;<a href=javascript:'.$this->function_name.'('.$page.');>&nbsp;'.$page.'&nbsp;</a>';//ajax方式
    					else
                        	$linkPage .= '&nbsp;<a href=\''.$url.'&'.$p.'='.$page.'\'>&nbsp;'.$page.'&nbsp;</a>';
                    }else{
                        break;
                    }
                }else{
                    if($this->totalPages != 1){
                        $linkPage .= '&nbsp;<span class=\'currentpage\'>'.$page.'</span>';
                    }
                }
           	  }
    		}
    		//jump page
    		if($this->isJumpPage){
    			$jump = '<input type=\'text\' id=\'jumppage\' value = \''.$this->nowPage.'\'>';
    			$jump .='<input type=\'button\' id=\'jumppagebutton\' value=\'GO\' onclick=\'go_page('.$this->nowPage.')\'>';
    			$jump .='<script>function go_page(page){var jumptopage = document.getElementById(\'jumppage\').value;var topage;';
    			$jump .='if(jumptopage==page||isNaN(jumptopage)){return false;}else{ ';
    			$jump .='if(jumptopage>'.$this->totalPages.'){topage = '.$this->totalPages.'}';
    			$jump .='else if(jumptopage<1){topage = 1;}else{topage = jumptopage;}';
    			if($this->AjaxType){
    				$jump .= $this->function_name.'(topage);';
    			}else{
    				$jump .='window.location.href = \''.$url.'&'.$p.'=\'+topage;';
    			}
    			$jump .='}}</script>';
    		}
            $pageStr=str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%jump%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$jump),$this->config['theme']);
            return $pageStr;
        }
    	
    }
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:
    上一篇:一个简单的memcache类 下一篇:PHP 二维数组分页处理
    Web大前端开发直播班

    相关文章推荐

    • ZF框架Db类的一些遍历结果集和对表操作的最基本用法• JQuery中Bind()事件用法分析_jquery• 极简的创建文件夹函数 • PHP读取文本文件并逐行输出该行使用最多的字符串及对应次数• php用正则判断是否为数字的方法
    1/1

    PHP中文网