正在为你努力加载中........

点滴记录 分享
3
2009-6
首页 >> php >> php 简单的分页

php 简单的分页

/**
*分页
* 
* 
* @param $url 链接地址
* @param $rows 每页显示的数量
* @param  $total 总页数
* 
* 
**/

function getpage($url,$rows=15,$total='',$page = 1 ){
	if(!$page){
		$page=1;
	}
	
	$totalpage=@ceil($total/$rows);
	$nextpage=$page+1;
	$uppage=$page-1;
	if($nextpage>$totalpage){
		$nextpage=$totalpage;
	}
	if($uppage<1){
		$uppage=1;
	}
	$s=$page-3;
	if($s<1){
		$s=1;
	}
	$b=$s;
	for($ii=0;$ii<6;$ii++){
		$b++;
	}
	if($b>$totalpage){
		$b=$totalpage;
	}
	for($j=$s;$j<=$b;$j++){
		if($j==$page){
			$show.=" <li><span>$j</span></li>";
		}else{
			$show.=" <li><a href=\"javascript:void(0)\" title=\"第{$j}页\" onclick=\"ajax_get($j)\">$j</a></li>";
		}
	}
	$showpage="<li><a href=\"javascript:void(0)\" title=\"首页\" class=\"pb\"><span>首页</span></li><li><a onclick=\"ajax_get($uppage)\">上一页</a></li>{$show}  <li><span><a href=\"javascript:void(0)\" title=\"下一页\" class=\"next\" onclick=\"ajax_get($nextpage)\">下一页</a></span></li> <li><a href=\"javascript:void(0)\" title=\"尾页\" class=\"pe\" onclick=\"ajax_get($totalpage)\">尾页</A></li>";
    if($totalpage>1){
		return $showpage;
	}
}
 
分页类加上省略号::
代码如下:
 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<div class="pageslist">
<?php 
$searchCount = 11;
$pageObject = new Page($searchCount ,10 ,''   );
$pageString =  $pageObject->show();
echo $pageString;
?>

</div>

<?php
//普通的分页类
class Page {
    
    
    // 分页栏每页显示的页数
    public $rollPage = 5;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow    ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
    // 默认分页变量名
    protected $varPage;

    protected $url ;

    protected $currentSiteDomain = '' ;
    /**
     * 架构函数
     * @access public
     * @param array $totalRows  总的记录数
     * @param array $listRows   每页显示记录数
     * @request_url string      请求地址  
     * @nowPage 当前的第几页
     * @varpage 分页的那个key 默认分页变量名
     */
    public function __construct($totalRows,$listRows=''  , $request_url = '' ,$nowPage = '',  $varpage = '') {
        $this->totalRows    =   $totalRows;
        $this->varPage      =   $varpage ? $varpage : 'page' ;
        if(!empty($listRows)) {
            $this->listRows =   intval($listRows);
        }
        $this->totalPages   =   ceil($this->totalRows/$this->listRows);//总页数
        $this->coolPages    =   ceil($this->totalPages/$this->rollPage);
        /*$this->nowPage      =   !empty($_REQUEST[$this->varPage])?intval($_REQUEST[$this->varPage]):1;*/
        $this->nowPage      =   $nowPage  ? $nowPage : (!empty($_REQUEST[$this->varPage])?intval($_REQUEST[$this->varPage]):1) ;
        if($this->nowPage<1){
            $this->nowPage  =   1;
        }elseif(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage  =   $this->totalPages;
        }
        $this->firstRow     =   $this->listRows*($this->nowPage-1);
        if($request_url && !empty($request_url)){
            $this->url = $request_url ;
        }else{
            $this->currentSiteDomain = 'http://'.$_SERVER['HTTP_HOST'];
            $this->getUrl(); 
        }
    }
    private function getUrl(){
        $url = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://';
        $url .= $_SERVER['HTTP_HOST'];
        $url = $url . $_SERVER['REQUEST_URI']; ;
        $data = parse_url ( $url ) ;
        if( (isset($data['query'])) AND  $data['query']){
            parse_str($data['query'] , $parameter);
        }
        $this->url = $this->currentSiteDomain . $data['path'] ;
        if(isset($parameter) && $parameter ){
            unset($parameter[$this->varPage]);
            $parameter = http_build_query($parameter);
            $this->url = $this->url . "?" . $parameter ;
        }
    }
    function get_page_url($page = 1 ){
        if(stripos($this->url , "?") === false ){
            $this->url = $this->url . "?&fromPage=1" ;
        }
        return $this->url."&{$this->varPage}={$page}";
    }
    /**
     * 分页显示输出
     * @access public
     */
    public function show() {
        if(0 == $this->totalRows) return '';
        $p              =   $this->varPage;
        $nowCoolPage    =   ceil($this->nowPage/$this->rollPage);
        //上下翻页字符串
        $upRow          =   $this->nowPage-1;
        $downRow        =   $this->nowPage+1;
        $upPage = '';
        if ($upRow>0){
            $upPage.="<a class=\"page\" href='{$this->get_page_url($upRow)}'>{$this->config['prev']}</a>";
        }else{
            $upPage     =   '';
        }

        if ($downRow <= $this->totalPages){
            
            $downPage = "<a class=\"page\"  href='{$this->get_page_url($downRow)}' >{$this->config['next']}</a>";
          
        }else{
            $downPage   =   '';
        }
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst   =   '';
            $prePage    =   '';
        }else{
            $preRow     =   $this->nowPage-$this->rollPage;
            $prePage = "<a  class=\"page\" href='{$this->get_page_url($preRow)}' >上{$this->rollPage}页</a>";
            
            $theFirst = "<a class=\"page\" href='{$this->get_page_url(1)}' >{$this->config['first']}</a>";
          //  $theFirst   =   "<a href='".str_replace('__PAGE__',1,$url)."' >".$this->config['first']."</a>";
        }
        if($nowCoolPage == $this->coolPages){
            $nextPage   =   '';
            $theEnd     =   '';
        }else{
            $nextRow    =   $this->nowPage+$this->rollPage;
            $theEndRow  =   $this->totalPages;
            
            //$nextPage   = "<li><a class=\"page\" href='{$this->get_page_url($nextRow)}' >下{$this->rollPage}页</a></li>";
            
            $theEnd = "<a class=\"page\" href='{$this->get_page_url($theEndRow)}' >{$this->config['last']}</a>";
        }
        // 1 2 3 4 5 ...
        $linkPage = "";
        if($this->nowPage >= $this->totalPages){
            $this->nowPage = $this->totalPages;
        }
        if($this->nowPage > $this->rollPage){
            $linkPage.="<a class=\"page\" href='{$this->get_page_url(1)}'>1</a>";
            $linkPage.="<a class=\"page\" href='{$this->get_page_url(2)}'>2</a>";
            $linkPage .="<span class=\"\">...</span>";
            for($kk = ($this->nowPage - 2 ) ; $kk < $this->nowPage ; $kk ++ ){
                $linkPage.="<a class=\"page\" href='{$this->get_page_url($kk)}'>{$kk}</a>";
            }
            unset($kk);
            //当前的页数
            $linkPage .="<span class=\"page active\">{$this->nowPage}</span>";
            $diffNum = ($this->totalPages) - ($this->nowPage);
            if($this->rollPage < $this->totalPages){
                if( $diffNum >3 ){
                    for($kk = $this->nowPage + 1  ; $kk <= $this->nowPage + 2 ; $kk ++ ){
                        $linkPage.="<a class=\"page\" href='{$this->get_page_url($kk)}'>{$kk}</a>";
                    }
                    $linkPage .="<span class=\"\">...</span>";
                }else{
                    unset($kk);
                    for($kk = $this->nowPage + 1  ; $kk <= $this->totalPages  ; $kk ++ ){
                        $linkPage.="<a class=\"page\" href='{$this->get_page_url($kk)}'>{$kk}</a>";
                    }
                }
            }
        }elseif($this->nowPage == $this->rollPage){
            for($kk = 1 ; $kk <= $this->rollPage+2; $kk ++ ){
                if($this->rollPage > $this->totalPages){
                    break;
                }
                if($this->nowPage == $kk){
                    $linkPage .="<span class=\"page active\">{$this->nowPage}</span>";
                }else{
                    $linkPage.="<a class=\"page\" href='{$this->get_page_url($kk)}'>{$kk}</a>"; 
                }
            }
            if($this->rollPage <= $this->totalPages){
                $linkPage .="<span class=\"\">...</span>";
            }
        }else{
            for($kk = 1 ; $kk <= $this->rollPage; $kk ++ ){
                if($kk > $this->totalPages){
                    break;
                }
                if($this->nowPage == $kk){
                    $linkPage .="<span class=\"page active\">{$this->nowPage}</span>";
                }else{
                    $linkPage.="<a class=\"page\" href='{$this->get_page_url($kk)}'>{$kk}</a>"; 
                }
            }
            if($this->rollPage <= $this->totalPages){
                $linkPage .="<span class=\"\">...</span>";
            }
        }
        return $upPage . $linkPage .  $downPage ;
    }
}
?>










<style>
.pageslist {
    font-size: 0;
    margin-bottom: 10px;
    text-align: center;
}
.pageslist a, .pageslist span {
    font-family: "Hiragino Sans GB";
    font-size: 14px;
    margin: 0 2px;
}
.pageslist .page {
    background: #fff none repeat scroll 0 0;
    border: 1px solid #eaeaea;
    color: #666;
    height: 36px;
    line-height: 36px;
    min-width: 36px;
    padding: 0 10px;
    text-align: center;
    transition: background-color 0.1s linear 0s;
    vertical-align: middle;
}
.pageslist .page.disabled {
    color: #e0e0e0;
}
.pageslist .page.disabled:hover {
    background: #fff none repeat scroll 0 0;
    border: 1px solid #eaeaea;
    color: #e0e0e0;
}
.pageslist .page.active {
    border: 1px solid #34b9be;
    color: #34b9be;
}
.pageslist .page:hover {
    background: #34b9be none repeat scroll 0 0;
    border: 1px solid #34b9be;
    color: #fff;
}
</style>
 
 效果如下:
分页.png
 
 

作者:一根烟的寂寞 分类:php
浏览:2192 评论:0

发表评论: