PHP分页类代码


参考:http://www.jb51.net/article/35229.htm

亲测可用,是一款比较完美的分页类。

测试运行结果裁图如下:



直接上代码:

pager.php

<?php   

class Pager {   
    private $pageSize = 5;   
    private $pageIndex;   
    private $totalNum;   

    private $totalPagesCount;   

    private $pageUrl;   
    private static $_instance;

    public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 5,$p_initNum=3,$p_initMaxNum=5) {   
        if (!isset ( $p_totalNum ) || !isset($p_pageIndex)) {   
            die ( "pager initial error" );   
        }   

        $this->totalNum = $p_totalNum;   
        $this->pageIndex = $p_pageIndex;   
        $this->pageSize = $p_pageSize;   
        $this->initNum=$p_initNum;   
        $this->initMaxNum=$p_initMaxNum;   
        $this->totalPagesCount= ceil($p_totalNum / $p_pageSize);   
        $this->pageUrl=$this->_getPageUrl();   

         $this->_initPagerLegal();   
    }   

       
  /**  
    * 获取去除page部分的当前URL字符串  
    *  
    * @return String URL字符串  
    */  
  private function _getPageUrl() {   
        $CurrentUrl = $_SERVER["REQUEST_URI"];   
        $arrUrl     = parse_url($CurrentUrl);   
        $urlQuery   = $arrUrl["query"];   

        if($urlQuery){   
            $urlQuery  = ereg_replace("(^|&)page=" . $this->pageIndex, "", $urlQuery);   
            $CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl);   

            if($urlQuery){   
                 $CurrentUrl.="&page";   
            }   
            else $CurrentUrl.="page";   

        } else {   
            $CurrentUrl.="?page";   
        }   

    return $CurrentUrl;   

  }   
  /*  
   *设置页面参数合法性  
   *@return void  
  */  
  private function _initPagerLegal()   
  {   
      if((!is_numeric($this->pageIndex)) ||  $this->pageIndex<1)   
      {   
          $this->pageIndex=1;   
      }elseif($this->pageIndex > $this->totalPagesCount)   
      {   
          $this->pageIndex=$this->totalPagesCount;   
      }   

         

  }   
//$this->pageUrl}={$i}   
//{$this->CurrentUrl}={$this->TotalPages}   
    public function GetPagerContent() {   
        $str = "<div class=\"Pagination\">";   
        //首页 上一页   
        if($this->pageIndex==1)   
        {   
            $str .="<a href='javascript:void(0)' class='tips' title='首页'>首页</a> "."\n";   
            $str .="<a href='javascript:void(0)' class='tips' title='上一页'>上一页</a> "."\n"."\n";   
        }else  
        {   
            $str .="<a href='{$this->pageUrl}=1' class='tips' title='首页'>首页</a> "."\n";   
                    $str .="<a href='{$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一页'>上一页</a> "."\n"."\n";   
        }   

           

        /*  

        除首末后 页面分页逻辑  

        */  
         //10页(含)以下   
         $currnt="";   
         if($this->totalPagesCount<=10)   
         {   

            for($i=1;$i<=$this->totalPagesCount;$i++)   

            {   
                       if($i==$this->pageIndex)   
                       {    $currnt=" class='current'";}   
                       else  
                       {    $currnt="";    }   
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;   
            }   
         }else                                //10页以上   
         {   if($this->pageIndex<3)  //当前页小于3   
             {   
                     for($i=1;$i<=3;$i++)   
                     {   
                         if($i==$this->pageIndex)   
                           {    $currnt=" class='current'";}   
                         else  
                         {    $currnt="";    }   
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;   
                     }   

                     $str.="<span class=\"dot\">……</span>"."\n";   

                 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1   
                 {   
                      $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;   

                 }   
             }elseif($this->pageIndex<=5)   //   5 >= 当前页 >= 3   
             {   
                 for($i=1;$i<=($this->pageIndex+1);$i++)   
                 {   
                      if($i==$this->pageIndex)   
                       {    $currnt=" class='current'";}   
                       else  
                       {    $currnt="";    }   
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;   

                 }   
                 $str.="<span class=\"dot\">……</span>"."\n";   

                 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1   
                 {   
                      $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;   

                 }   

             }elseif(5<$this->pageIndex  &&  $this->pageIndex<=$this->totalPagesCount-5 )             //当前页大于5,同时小于总页数-5   

             {   

                 for($i=1;$i<=3;$i++)   
                 {   
                     $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;   
                 }   
                  $str.="<span class=\"dot\">……</span>";                
                 for($i=$this->pageIndex-1 ;$i<=$this->pageIndex+1 && $i<=$this->totalPagesCount-5+1;$i++)   
                 {   
                       if($i==$this->pageIndex)   
                       {    $currnt=" class='current'";}   
                       else  
                       {    $currnt="";    }   
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;   
                 }   
                 $str.="<span class=\"dot\">……</span>";   

                 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)   
                 {   
                      $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;   

                 }   
             }else  
             {   

                  for($i=1;$i<=3;$i++)   
                 {   
                     $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;   
                 }   
                  $str.="<span class=\"dot\">……</span>"."\n";   

                  for($i=$this->totalPagesCount-5;$i<=$this->totalPagesCount;$i++)//功能1   
                 {   
                       if($i==$this->pageIndex)   
                       {    $currnt=" class='current'";}   
                       else  
                       {    $currnt="";    }   
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;   

                 }   
            }          

         }   

            

            
        /*  

        除首末后 页面分页逻辑结束  

        */  

        //下一页 末页   
        if($this->pageIndex==$this->totalPagesCount)   
        {      
            $str .="\n"."<a href='javascript:void(0)' class='tips' title='下一页'>下一页</a>"."\n" ;   
            $str .="<a href='javascript:void(0)' class='tips' title='末页'>末页</a>"."\n";   

               
        }else  
        {   
            $str .="\n"."<a href='{$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一页'>下一页</a> "."\n";   
            $str .="<a href='{$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末页'>末页</a> "."\n" ;   
        }          

        $str .= "</div>";   
        return $str;   
    }   
  
}   
?>   

pager.css

/* CSS Document */

body,html{ padding:0px; margin:0px; color:#333333; font-family:"宋体",Arial,Lucida,Verdana,Helvetica,sans-serif; font-size:12px; line-height:150%;}   

h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;}   

/**前台分页样式**/  

.Pagination {margin:10px 0 0;padding:5px 0;text-align:rightright; height:20px; line-height:20px; font-family:Arial, Helvetica, sans-serif,"宋体";}   

.Pagination a {margin-left:2px;padding:2px 7px 2px;}   

.Pagination .dot{ border:medium none; padding:4px 8px}   

.Pagination a:link, .Pagination a:visited {border:1px solid #dedede;color:#696969;text-decoration:none;}   

.Pagination a:hover, .Pagination a:active, .Pagination a.current:link, .Pagination a.current:visited {border:1px solid #dedede;color:#fff; 
background-color:#ff6600; background-image:none; border:#ff6600 solid 1px;}   

.Pagination .selectBar{ border:#dedede solid 1px; font-size:12px; width:95px; height:21px; line-height:21px; margin-left:10px; display:inline}   

.Pagination a.tips{_padding:4px 7px 1px;}   

test.php

<!DOCTYPE HTML>
<html>
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
<title>分页类测试</title>   
<link href="pager.css" type="text/css" rel="stylesheet" />   
</head>   
<body>

  <?php 
   		//header('content-type:text/html;charset=utf-8');
		require_once '../class/mysqlfunction.php';
		require_once '../class/mysqlconfig.php';
	
		$db = new mysql();
		$link = $db->connect2();
		//var_dump($link);
		$table = "***";
		$num = $db->totalnum($table);
		//echo $num;
 
		 include "pager.php";   
		 $CurrentPage=isset($_GET['page'])?$_GET['page']:1;
		 $pageSize = 1; 
		 $myPage=new pager($num,intval($CurrentPage),$pageSize);   
		 $pageStr= $myPage->GetPagerContent();
		 echo $pageStr;
		 
		 //显示分页后表单的内容
		 $offset = ($CurrentPage - 1) * $pageSize;
		 $info_num = $pageSize;
		 $info_sql = "select * from *** order by id desc limit $offset,$info_num";
		 $rows = $db->fetchAll($info_sql);
		 foreach($rows as $key=>$val){
		 	echo $val['title']."<br>".$val['case_description']."<br>";
		 }
    ?>
	
	<?php 
		$link = $db->close($link);
	?>

</body>   
</html>  





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值