首頁 >php教程 >PHP源码 >ThinkPHP抽出来的分页类

ThinkPHP抽出来的分页类

PHP中文网
PHP中文网原創
2016-05-25 17:12:001038瀏覽


代码片段

                       

                       

1. [图片] default_theme.jpg    

           

ThinkPHP抽出来的分页类

                                               

                       

2. [图片] theme_1.jpg    

           

ThinkPHP抽出来的分页类

                                               

                               

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


<?php
require_once &#39;mysql.class.php&#39;;
require_once &#39;page.class.php&#39;;
require_once &#39;config.php&#39;;

$mysql = new MySQL(DB_HOST_NAME,DB_USER_NAME,DB_PASS_WORD,DB_DATABASE_NAME);

$pagenum = isset ( $_REQUEST [&#39;page&#39;] ) ? $_REQUEST [&#39;page&#39;] : 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 = &#39;yourself&#39;;
$p->setConfig(&#39;theme&#39;,&#39;%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%&#39;);

$sql = "select * from ajax limit ".($pagenum-1)*$page_row.",".$page_row;
$result = $mysql->query ( $sql );

$return_data[&#39;data&#39;] = $result;
$return_data[&#39;page&#39;] = $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 = &#39;ajax_page&#39;;
	// 分页显示定制
    protected $config = array(&#39;header&#39;=>&#39;条记录&#39;,&#39;prev&#39;=>&#39;上一页&#39;,&#39;next&#39;=>&#39;下一页&#39;,&#39;first&#39;=>&#39;首页&#39;,&#39;last&#39;=>&#39;末页&#39;,&#39;theme&#39;=>&#39; %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end% %jump%&#39;);
	/**
     +----------------------------------------------------------
     * 
     +----------------------------------------------------------
     * @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[&#39;page&#39;])?$_REQUEST[&#39;page&#39;]: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 &#39;&#39;;
		$p = &#39;page&#39;;
		$nowCoolPage      = ceil($this->nowPage/$this->rollPage);
		$request_url = $_SERVER[&#39;REQUEST_URI&#39;];
		
		if(strpos($request_url,&#39;?&#39;)){
			$url  =  $request_url;
		}else{
			$url  =  $request_url.&#39;?&#39;;
		}
		
        $parse = parse_url($url);
        if(isset($parse[&#39;query&#39;])) {
            parse_str($parse[&#39;query&#39;],$params);
            unset($params[$p]);
            $url   =  $parse[&#39;path&#39;].&#39;?&#39;.http_build_query($params);
        }
        
        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        if ($upRow>0){         
			if($this->AjaxType)
				$upPage=&#39;<a href=javascript:&#39;.$this->function_name.&#39;(&#39;.$upRow.&#39;);>&#39;.$this->config[&#39;prev&#39;].&#39;</a>&#39;;//ajax方式
			else
				$upPage=&#39;<a href=\&#39;&#39;.$url.&#39;&&#39;.$p.&#39;=&#39;.$upRow.&#39;\&#39;>&#39;.$this->config[&#39;prev&#39;].&#39;</a>&#39;;
        }else{
            $upPage=&#39;&#39;;
        }

        if ($downRow <= $this->totalPages){
			if($this->AjaxType)
				$downPage=&#39;<a href=javascript:&#39;.$this->function_name.&#39;(&#39;.$downRow.&#39;);>&#39;.$this->config[&#39;next&#39;].&#39;</a>&#39;;//ajax方式
			else
				$downPage=&#39;<a href=\&#39;&#39;.$url.&#39;&&#39;.$p.&#39;=&#39;.$downRow.&#39;\&#39;>&#39;.$this->config[&#39;next&#39;].&#39;</a>&#39;;
        }else{
            $downPage=&#39;&#39;;
        }
        // << < > >>
        if($this->nowPage == 1){
            $theFirst = &#39;&#39;;
            $prePage = &#39;&#39;;
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
			if($this->AjaxType)
				$theFirst = &#39;<a href=javascript:&#39;.$this->function_name.&#39;(1);>&#39;.$this->config[&#39;first&#39;].&#39;</a>&#39;;//ajax方式
			else
            	$theFirst = &#39;<a href=\&#39;&#39;.$url.&#39;&&#39;.$p.&#39;=1\&#39; >&#39;.$this->config[&#39;first&#39;].&#39;</a>&#39;;
        }
        if($this->nowPage == $this->totalPages){
            $nextPage = &#39;&#39;;
            $theEnd=&#39;&#39;;
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
			if($this->AjaxType)
				$theEnd = &#39;<a href=javascript:&#39;.$this->function_name.&#39;(&#39;.$theEndRow.&#39;);>&#39;.$this->config[&#39;last&#39;].&#39;</a>&#39;;//ajax方式
			else
            	$theEnd = &#39;<a href=\&#39;&#39;.$url.&#39;&&#39;.$p.&#39;=&#39;.$theEndRow.&#39;\&#39; >&#39;.$this->config[&#39;last&#39;].&#39;</a>&#39;;
        }
        // 1 2 3 4 5
		if($this->isShowLinkPage){
			$linkPage = &#39;&#39;;
        	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 .= &#39; <a href=javascript:&#39;.$this->function_name.&#39;(&#39;.$page.&#39;);> &#39;.$page.&#39; </a>&#39;;//ajax方式
					else
                    	$linkPage .= &#39; <a href=\&#39;&#39;.$url.&#39;&&#39;.$p.&#39;=&#39;.$page.&#39;\&#39;> &#39;.$page.&#39; </a>&#39;;
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= &#39; <span class=\&#39;currentpage\&#39;>&#39;.$page.&#39;</span>&#39;;
                }
            }
       	  }
		}
		//jump page
		if($this->isJumpPage){
			$jump = &#39;<input type=\&#39;text\&#39; id=\&#39;jumppage\&#39; value = \&#39;&#39;.$this->nowPage.&#39;\&#39;>&#39;;
			$jump .=&#39;<input type=\&#39;button\&#39; id=\&#39;jumppagebutton\&#39; value=\&#39;GO\&#39; onclick=\&#39;go_page(&#39;.$this->nowPage.&#39;)\&#39;>&#39;;
			$jump .=&#39;<script>function go_page(page){var jumptopage = document.getElementById(\&#39;jumppage\&#39;).value;var topage;&#39;;
			$jump .=&#39;if(jumptopage==page||isNaN(jumptopage)){return false;}else{ &#39;;
			$jump .=&#39;if(jumptopage>&#39;.$this->totalPages.&#39;){topage = &#39;.$this->totalPages.&#39;}&#39;;
			$jump .=&#39;else if(jumptopage<1){topage = 1;}else{topage = jumptopage;}&#39;;
			if($this->AjaxType){
				$jump .= $this->function_name.&#39;(topage);&#39;;
			}else{
				$jump .=&#39;window.location.href = \&#39;&#39;.$url.&#39;&&#39;.$p.&#39;=\&#39;+topage;&#39;;
			}
			$jump .=&#39;}}</script>&#39;;
		}
        $pageStr=str_replace(
        array(&#39;%header%&#39;,&#39;%nowPage%&#39;,&#39;%totalRow%&#39;,&#39;%totalPage%&#39;,&#39;%upPage%&#39;,&#39;%downPage%&#39;,&#39;%first%&#39;,&#39;%prePage%&#39;,&#39;%linkPage%&#39;,&#39;%nextPage%&#39;,&#39;%end%&#39;,&#39;%jump%&#39;),
        array($this->config[&#39;header&#39;],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$jump),$this->config[&#39;theme&#39;]);
        return $pageStr;
    }
	
}

                               

                   

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn