PHP分页类

I have write one pagination class using PHP.  This is useful for you reference.

<? php
class  Paging {
    
private   $total_rows ;
    
private   $total_pages ;
    
private   $rows_per_page ;
    
private   $pre_sign // the previous page sign
     private   $next_sign // the next page sign
     private   $first_sign // the first page sign
     private   $last_sign // the last page sign
     private   $seperater // the page number seperater
     private   $current_page ;
    
private   $start_page ;
    
private   $num_per_page // how many page number to show
    
    
/* *
     * Construct function.
     * @param int $total_rows the total rows number.
     * @param int $rows_per_page how many row to display per page.
     
*/
    
public   function  __construct( $total_rows ,   $rows_per_page ,   $num_page   =   10 ) {
        
$this -> total_rows  =   $total_rows ;
        
$this -> total_pages  =   ceil ( $total_rows   /   $rows_per_page );
        
$this -> rows_per_page  =   $rows_per_page ;
        
$this -> current_page  =   isset ( $_GET [ ' page ' ])  ?   $_GET [ ' page ' :   1 ;
        
$this -> num_per_page  =   $num_page ;
        
        
$this -> pre_sign  =   ' < ' ;
        
$this -> next_sign  =   ' > ' ;
        
$this -> first_sign  =   ' << ' ;
        
$this -> last_sign  =   ' >> ' ;
        
$this -> seperater  =   ' | ' ;
    }
    
    
/* *
     * Show paging info.
     
*/
    
public   function  showPaging() {
        
if  ( $this -> total_rows  >   $this -> rows_per_page) {
            
$this -> start_page  =   $this -> getStartPage();
            
$end_page   =   $this -> start_page  +   $this -> num_per_page  -   1 ;
            
            
if  ( $end_page   >   $this -> total_pages) { // adjust the end page, start page
                 $end_page   =   $this -> total_pages;
                
$this -> start_page  =   $end_page   -   $this -> num_per_page  +   1 ;
            }
            
$pages   =   $this -> addLinkInfo( $this -> start_page ,   $end_page );
        
            
$paging   =   implode ( $this -> seperater ,   $pages );
        }
        
else  {
            
$paging   =   '' ;
        }
        
        
return   $paging ;
    }
    
    
/* *
     * Gets start page number.
     * @return int $start_page the start page number.
     
*/
    
private   function  getStartPage() {
        
$start   =   NULL ;
        
        
$start   =  ( 5   <   $this -> current_page)  ?  ( $this -> current_page  -   5 :   1 ;
        
        
return  ( $this -> total_pages  >   $start ?   $start   :  ( $this -> total_pages  -   $this -> num_per_page  +   1 );
    }
    
    
/* *
     * Sets the pre page sign.
     
*/
    
public   function  setPreSign( $pre_sign ) {
        
$this -> pre_sign  =   $pre_sign ;
    }
    
    
/* *
     * Sets the next page sign.
     
*/
    
public   function  setNextSign( $next_sign ) {
        
$this -> next_sign  =   $next_sign ;
    }
    
    
/* *
     * Sets the first page sign.
     
*/
    
public   function  setFirstSign( $first_sign ) {
        
$this -> first_sign  =   $first_sign ;
    }
    
    
/* *
     * Sets the last page sign.
     
*/
    
public   function  setLastSign( $last_sign ) {
        
$this -> last_sign  =   $last_sign ;
    }
    
    
/* *
     * Sets the page seperator sign.
     
*/
    
public   function  setSeperator( $seperator ) {
        
$this -> seperater  =   $seperator ;
    }
    
    
/* *
     * Adds page link info.
     * @param int $start_page the start page number.
     * @param int $end_page the end page number.
     
*/
    
private   function  addLinkInfo( $start_page ,   $end_page ) {
        
$page_with_link   =   NULL ;
        
$paging   =   array ();
        
        
if  ( 1   !=   $start_page ) {
            
$paging []  =   $this -> addPageLink( $this -> first_sign ,   ' first ' );  // add first page link
        }
        
if  ( 1   <   $this -> current_page) {
            
$paging []  =   $this -> addPageLink( $this -> current_page  -   1 ,   ' previous ' );  // add previous page link
        }
        
        
for  ( $i   =   $start_page $i   <=   $end_page ++ $i ) {
            
if  ( $i   !=   $this -> current_page) {
                
$page_with_link   =   $this -> addPageLink( $i );
            }
            
else  {
                
$page_with_link   =   $i ;
            }
            
$paging []  =   $page_with_link ;
        }
        
        
if  ( $this -> current_page  <   $this -> total_pages) {
            
$paging []  =   $this -> addPageLink( $this -> current_page  +   1 ,   ' next ' );  // add next page link
            
            
if  ( $this -> total_pages  >   $end_page ) {  // need add the last page link
                 $paging []  =   $this -> addPageLink( $this -> last_sign ,   ' last ' );
            }
        }
        

        
return   $paging ;
    }
    
    
/* *
     * Adds link info to page $page_num.
     * @param int $page_num
     * @return string page number with link info.
     
*/
    
private   function  addPageLink( $page_num ,   $add_sign   =   '' ) {
        
$current_file   =   basename ( $_SERVER [ ' PHP_SELF ' ]);
        
        
if  ( ' first '   ==   $add_sign ) {
            
$query_string   =   $this -> getQueryString( 1 );
            
$link   =   <<< END
            
< a href = " $current_file?$query_string " > $page_num </ a >
END ;
        }
        
else   if  ( ' last '   ==   $add_sign ) {
            
$query_string   =   $this -> getQueryString( $this -> total_pages);
            
$link   =   <<< END
            
< a href = " $current_file?$query_string " > $page_num </ a >
END ;
        }
        
else   if  ( ' previous '   ==   $add_sign ){
            
if  ( 0   >=   $page_num ) {
                
$page_num   =   1 ;
            }
            
$query_string   =   $this -> getQueryString( $page_num );
            
$link   =   <<< END
            
< a href = " $current_file?$query_string " > $this -> pre_sign </ a >
END ;
        }
        
else   if  ( ' next '   ==   $add_sign ) {
            
if  ( $page_num   >   $this -> total_pages) {
                
$page_num   =   $this -> total_pages;
            }
            
$query_string   =   $this -> getQueryString( $page_num );
            
$link   =   <<< END
            
< a href = " $current_file?$query_string " > $this -> next_sign </ a >
END ;
        }
        
else  {
            
$query_string   =   $this -> getQueryString( $page_num );
            
$link   =   <<< END
            
< a href = " $current_file?$query_string " > $page_num </ a >
END ;
        }
        
        
return   $link ;
    }
    
    
/* *
     * Gets the query string from the URL.
     * @param int $page set the current page number.
     * @return string new query string.
     
*/
    
private   function  getQueryString( $page ) {
        
$query_string   =   $_SERVER [ ' QUERY_STRING ' ];
        
$query_array   =   NULL ;
        
parse_str ( $query_string ,   $query_array );
        
$query_array [ ' page ' =   $page // set page num
        
        
return   http_build_query ( $query_array );
    }
}
$page   =   new  Paging( 1000 ,   10 );
$page -> setFirstSign( ' first ' ); // set the first page sign, default is '<<'
$page -> setPreSign( ' pre ' )); // set the pre page sign, default is '<'
$page -> setNextSign( ' next ' )); // set the next page sign, default is '>'
$page -> setLastSign( ' last ' )); // set the last page sign, default is '>>'
$page -> setSeperator( ' ][ ' )); // set the first page sign, default is '|'
echo   ' [ '   .   $page -> showPaging()  .   ' ] ' ;
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值