PHP缓存文件的分页

<? php



class  Cache_Page {
    
var   $data_ary   =   array ();

    
/* *
     * 页面输出结果
     *
     * @var string
     
*/
    
var   $output   =   '' ;

    
/* *
     * 使用该类的文件,默认为 PHP_SELF
     *
     * @var string
     
*/
    
var   $file ;

    
/* *
     * 页数传递变量,默认为 'p'
     *
     * @var string
     
*/
    
var   $page_var   =   " p " ;

    
/* *
     * 页面大小,每页显示多少个记录
     *
     * @var integer
     
*/
    
var   $num_per_page   =   10 ;

    
/* *
     * 当前页面
     *
     * @var ingeger
     
*/
    
var   $current_page ;

    
/* *
     * 要传递的变量数组
     *
     * @var array
     
*/
    
var   $var_str ;

    
/* *
     * 总页数
     *
     * @var integer
     
*/
    
var   $total_pages ;
    
/* *
     * LIMIT 语句中的offset
     *
     * @var integer
     
*/
    
var   $offset   =   0 ;
    
/* *
     * 分页显示几个链接 如:第 1 2 3 4 5 6 7 8 页
     *
     * @var integer
     
*/
    
var   $link_num   =   8 ;

    
var   $record_nums   =   0 ;
    
    
var   $prev_page   =   0 ;
    
var   $prev_page_url   =   '' ;
    
var   $next_page   =   0 ;
    
var   $next_page_url   =   '' ;

    
function  Cache_Page( $ary )
    {
        
$this -> data_ary  =  ( isset ( $ary &&   is_array ( $ary ))  ?   $ary   :   array ();
    }

    
/* *
     * 分页设置
     *
     * @access public
     * @param int $num_per_page 页面大小
     * @param int $record_nums    总记录数
     * @param int $current  当前页数,默认会自动读取
     * @return void
     
*/
    
function  set( $record_nums   =   null ,   $current_page   =   false ,   $unit = ' 条记录 ' ) {
        
$record_nums   =  ( is_null ( $record_nums ))  ?   $this -> RecordCount( $this -> data_ary)  :   intval ( $record_nums );

        
if  ( ! $current_page ) {
            
$current_page   =  ( isset ( $_GET [ $this -> page_var])  &&   is_numeric ( $_GET [ $this -> page_var])  &&   $_GET [ $this -> page_var]  >   0 ?   intval ( $_GET [ $this -> page_var])  :  (( isset ( $_POST [ $this -> page_var])  &&   is_numeric ( $_POST [ $this -> page_var])  &&   $_POST [ $this -> page_var]  >   0 ?   intval ( $_POST [ $this -> page_var])  :   1 );
        }
        
if  ( $current_page   ==   0 ) {
            
$current_page   =   1 ;
        } 
else  {
            
$current_page   =   floor ( $current_page );     //  防止有人故意捣乱
        }

        
if  ( $current_page   >   0 ) {
            
$total_pages   =  ( $record_nums   >   0 ?   ceil ( $record_nums / $this -> num_per_page)  :   1 ; // 总页数
             if  ((int) $current_page   >   $total_pages ) {  //  防止有人故意将页数输入太大
                 $current_page   =   $total_pages ;
            }
            
$current_page   =   floor ( $current_page );     //  防止有人故意捣乱
             $offset         =  ( $current_page - 1 *   $this -> num_per_page;
        } 
else  {     //  无翻页参数则默认为第1页
             $offset         =   0 ;
            
$current_page   =   1 ;
            
$total_pages    =   1 ;
        }


        
$this -> current_page  =   $current_page ;
        
$this -> offset        =   $offset ;
        
$this -> total_pages   =   $total_pages ;

        
if  ( $record_nums   ==   0   ||   $total_pages   ==   1 ) {
            
$this -> output  =   '' ;
            
return ;
        }
        
if  ( ! $this -> file ) { $this -> file   =   $_SERVER [ ' PHP_SELF ' ];}

        
$start   =  ( $current_page - round ( $this -> link_num / 2 ))  >   0   ?  ( $current_page - round ( $this -> link_num / 2 ))  :   1 ;
        
$end     =  ( $current_page + round ( $this -> link_num / 2 ))  <   $total_pages   ?  ( $current_page + round ( $this -> link_num / 2 ))  :   $total_pages ;
        
if  ( $current_page   <   round ( $this -> link_num / 2 )) {
            
$end   =  (  $this -> link_num  >   $total_pages  )  ?   $total_pages   :   $this -> link_num; 
        }

        
if  ( $current_page   !=   1 ) {
            
$this -> prev_page      =   $current_page   -   1 ;
            
$this -> prev_page_url  =   $this -> file   .   ' ? '   .   $this -> page_var  .   ' = '   .   $this -> prev_page  .  ( $this -> var_str);
        } 
elseif  ( $current_page   =   1 ) {
            
$this -> prev_page      =   0 ;
            
$this -> prev_page_url  =   '' ;
        }

        
$page_list_ary   =   array ();
        
for  ( $t   =   $start $t   <=   $end $t ++ ) {
            
$page_list_ary []  =  ( $current_page == $t ?   ' <font color="red"><b> '   .   $t   .   ' </b></font> '   :   ' <a href=" '   .   $this -> file   .   ' ? '   .   $this -> page_var  .   ' = '   .   $t   .  ( $this -> var_str)  .   ' " target="_self"> '   .   $t   .   ' </a> ' ;
        }
        
$this -> output  .=   implode ( ' &nbsp;|&nbsp; ' ,   $page_list_ary );

        
if  ( $current_page   !=   $total_pages ) {
            
$this -> next_page      =   $current_page   +   1 ;
            
$this -> next_page_url  =   $this -> file   .   ' ? '   .   $this -> page_var  .   ' = '   .   $this -> next_page  .  ( $this -> var_str);
        } 
elseif  ( $current_page   =   $total_pages ) {
            
$this -> next_page      =   0 ;
            
$this -> next_page_url  =   '' ;
        }

        
// select下拉框直接跳转
         $this -> output  .=   " &nbsp;&nbsp;转到第<select οnchange="location=' "   .   $this -> file   .   ' ? '   .   $this -> page_var  .   " ='+this.options[this.selectedIndex].value+' "   .  ( $this -> var_str)  .   " '" align=absmiddle  style="font-size:8pt;border: 1px solid #999999;"> " ;
        
$show_all      =   200 ;
        
$slice_start   =   5 ;
        
$slice_end     =   5 ;
        
$percent       =   20 ;
        
$range         =   10 ;
        
if  ( $total_pages   <   $show_all ) {
            
$pages   =   range ( 1 ,   $total_pages );
        } 
else  {
            
$pages   =   array ();

            
//  Always show first X pages
             for  ( $i   =   1 $i   <=   $slice_start $i ++ ) {
                
$pages []  =   $i ;
            }

            
//  Always show last X pages
             for  ( $i   =   $total_pages   -   $slice_end $i   <=   $total_pages $i ++ ) {
                
$pages []  =   $i ;
            }

            
//  garvin: Based on the number of results we add the specified $percent percentate to each page number,
            // so that we have a representing page number every now and then to immideately jump to specific pages.
            // As soon as we get near our currently chosen page ($pageNow - $range), every page number will be
            // shown.

             $i   =   $slice_start ;
            
$x   =   $total_pages   -   $slice_end ;
            
$met_boundary   =   false ;
            
while ( $i   <=   $x ) {
                
if  ( $i   >=  ( $current_page   -   $range &&   $i   <=  ( $current_page   +   $range )) {
                    
//  If our pageselector comes near the current page, we use 1 counter increments
                     $i ++ ;
                    
$met_boundary   =   true ;
                } 
else  {
                    
//  We add the percentate increment to our current page to hop to the next one in range
                     $i   =   $i   +   floor ( $total_pages   /   $percent );

                    
//  Make sure that we do not cross our boundaries.
                     if  ( $i   >  ( $current_page   -   $range &&   ! $met_boundary ) {
                        
$i   =   $current_page   -   $range ;
                    }
                }

                
if  ( $i   >   0   &&   $i   <=   $x ) {
                    
$pages []  =   $i ;
                }
            }

            
//  Since because of ellipsing of the current page some numbers may be double,
            // we unify our array:

             sort ( $pages );
            
$pages   =   array_unique ( $pages );
        }
        
$this -> output  .=   ' <option value=" '   .   $current_page   .   ' "> '   .   $current_page   .   ' </option> '   .  NL;
        
foreach  ( $pages   AS   $i ) {
            
if  ( $i   ==   $current_page ) {
                
$selected   =   ' selected="selected" style="font-weight: bold" ' ;
            } 
else  {
                
$selected   =   '' ;
            }
            
$this -> output  .=   ' <option  '   .   $selected   .   '  value=" '   .   $i   .   ' "> '   .   $i   .   " </option> " ;
        }

        
$this -> output  .=   '  </select>页 ' ;


        
// 显示每页记录数及记录总数
         $this -> output  =   ' <span id=page_string>共 '   .   $record_nums   .   $unit   .   ' /本页[ '   .   $current_page   .   ' / '   .   $total_pages   .   ' '   .   $this -> output  .   ' </span> ' ;

    }
    
function  getPageData()
    {
        
reset ( $this -> data_ary);
        
krsort ( $this -> data_ary);
        
$i   =   0 ;
        
$ary   =   array ();
        
while  ( list ( $key ,   $val =   each ( $this -> data_ary)) {
            
if  ( $i   >=   $this -> offset  &&   $i   <  ( $this -> offset  +   $this -> num_per_page)) {
                
$ary [ $key =   $val ;
            }
            
$i ++ ;
        }
        
return   $ary ;
    }

    
function  setNumPerPage( $num_per_page )
    {
        
$this -> num_per_page  =   intval ( $num_per_page );
    }

    
/* *
     * 要传递的变量设置
     *
     * @access public
     * @param array $data   要传递的变量,用数组来表示,参见上面的例子
     * @return void
     
*/     
    
function  setVar( $data ,   $only_url   =   false ) {
        
if  ( is_array ( $data &&   count ( $data >   0 ) {
            
foreach  ( $data   as   $k   =>   $v ) {
                
$this -> var_str   .=   ' &amp; '   .   $k   .   ' = '   .   urlencode ( $v );
                
if  ( ! $only_url ) {
                    
$this -> data_ary   =   $this -> getColumnAry( $this -> data_ary ,   $k ,   $v );
                }
            }
        }
    }
    
/* *
     * 页面的接收变量名设置,page=3, p=3
     *
     * @access public
     * @param string $page_var 要传递的变量,如page
     * @return void
     
*/     
    
function  setPageVar( $page_var ) {
        
$this -> page_var  =   $page_var ;
    }
    
function  setLinkNum( $number )
    {
        
$this -> link_num  =   intval ( $number );
    }

    
function  getColumnAry( $data_ary ,   $column_name ,   $column_value )
    {
        
reset ( $data_ary );
        
$ary   =   array ();
        
while  ( list ( $key ,   $val =   each ( $data_ary )) {
            
if  ( array_key_exists ( $column_name ,   $val &&   $val [ $column_name ==   $column_value ) {
                
$ary [ $key =   $val ;
            }
        }
        
return   $ary ;
    }


    
/* *
     * 分页结果输出
     *
     * @access public
     * @param bool $return 为真时返回一个字符串,否则直接输出,默认直接输出
     * @return string
     
*/
    
function  output( $return   =   false ) {
        
if  ( $return ) {
            
return   $this -> output;
        } 
else  {
            
echo   $this -> output;
        }
    }
    
function  RecordCount()
    {
        
$this -> record_nums  =   count ( $this -> data_ary);
        
return   $this -> record_nums;
    }

    
function  getOffset()
    {
        
return   $this -> offset;
    }
    
function  getTotalPages()
    {
        
return   $this -> total_pages;
    }
    
function  getCurrentPage()
    {
        
return   $this -> current_page;
    }
<? php
/*
+--------------------------------------------------------------------------
| 缓存文件的分页
| ============================
| by LiuCheng  2006.2
+--------------------------------------------------------------------------
*/     


class  Cache_Page {
    
var   $data_ary   =   array ();

    
/* *
     * 页面输出结果
     *
     * @var string
     
*/
    
var   $output   =   '' ;

    
/* *
     * 使用该类的文件,默认为 PHP_SELF
     *
     * @var string
     
*/
    
var   $file ;

    
/* *
     * 页数传递变量,默认为 'p'
     *
     * @var string
     
*/
    
var   $page_var   =   " p " ;

    
/* *
     * 页面大小,每页显示多少个记录
     *
     * @var integer
     
*/
    
var   $num_per_page   =   10 ;

    
/* *
     * 当前页面
     *
     * @var ingeger
     
*/
    
var   $current_page ;

    
/* *
     * 要传递的变量数组
     *
     * @var array
     
*/
    
var   $var_str ;

    
/* *
     * 总页数
     *
     * @var integer
     
*/
    
var   $total_pages ;
    
/* *
     * LIMIT 语句中的offset
     *
     * @var integer
     
*/
    
var   $offset   =   0 ;
    
/* *
     * 分页显示几个链接 如:第 1 2 3 4 5 6 7 8 页
     *
     * @var integer
     
*/
    
var   $link_num   =   8 ;

    
var   $record_nums   =   0 ;
    
    
var   $prev_page   =   0 ;
    
var   $prev_page_url   =   '' ;
    
var   $next_page   =   0 ;
    
var   $next_page_url   =   '' ;

    
function  Cache_Page( $ary )
    {
        
$this -> data_ary  =  ( isset ( $ary &&   is_array ( $ary ))  ?   $ary   :   array ();
    }

    
/* *
     * 分页设置
     *
     * @access public
     * @param int $num_per_page 页面大小
     * @param int $record_nums    总记录数
     * @param int $current  当前页数,默认会自动读取
     * @return void
     
*/
    
function  set( $record_nums   =   null ,   $current_page   =   false ,   $unit = ' 条记录 ' ) {
        
$record_nums   =  ( is_null ( $record_nums ))  ?   $this -> RecordCount( $this -> data_ary)  :   intval ( $record_nums );

        
if  ( ! $current_page ) {
            
$current_page   =  ( isset ( $_GET [ $this -> page_var])  &&   is_numeric ( $_GET [ $this -> page_var])  &&   $_GET [ $this -> page_var]  >   0 ?   intval ( $_GET [ $this -> page_var])  :  (( isset ( $_POST [ $this -> page_var])  &&   is_numeric ( $_POST [ $this -> page_var])  &&   $_POST [ $this -> page_var]  >   0 ?   intval ( $_POST [ $this -> page_var])  :   1 );
        }
        
if  ( $current_page   ==   0 ) {
            
$current_page   =   1 ;
        } 
else  {
            
$current_page   =   floor ( $current_page );     //  防止有人故意捣乱
        }

        
if  ( $current_page   >   0 ) {
            
$total_pages   =  ( $record_nums   >   0 ?   ceil ( $record_nums / $this -> num_per_page)  :   1 ; // 总页数
             if  ((int) $current_page   >   $total_pages ) {  //  防止有人故意将页数输入太大
                 $current_page   =   $total_pages ;
            }
            
$current_page   =   floor ( $current_page );     //  防止有人故意捣乱
             $offset         =  ( $current_page - 1 *   $this -> num_per_page;
        } 
else  {     //  无翻页参数则默认为第1页
             $offset         =   0 ;
            
$current_page   =   1 ;
            
$total_pages    =   1 ;
        }


        
$this -> current_page  =   $current_page ;
        
$this -> offset        =   $offset ;
        
$this -> total_pages   =   $total_pages ;

        
if  ( $record_nums   ==   0   ||   $total_pages   ==   1 ) {
            
$this -> output  =   '' ;
            
return ;
        }
        
if  ( ! $this -> file ) { $this -> file   =   $_SERVER [ ' PHP_SELF ' ];}

        
$start   =  ( $current_page - round ( $this -> link_num / 2 ))  >   0   ?  ( $current_page - round ( $this -> link_num / 2 ))  :   1 ;
        
$end     =  ( $current_page + round ( $this -> link_num / 2 ))  <   $total_pages   ?  ( $current_page + round ( $this -> link_num / 2 ))  :   $total_pages ;
        
if  ( $current_page   <   round ( $this -> link_num / 2 )) {
            
$end   =  (  $this -> link_num  >   $total_pages  )  ?   $total_pages   :   $this -> link_num; 
        }

        
if  ( $current_page   !=   1 ) {
            
$this -> prev_page      =   $current_page   -   1 ;
            
$this -> prev_page_url  =   $this -> file   .   ' ? '   .   $this -> page_var  .   ' = '   .   $this -> prev_page  .  ( $this -> var_str);
        } 
elseif  ( $current_page   =   1 ) {
            
$this -> prev_page      =   0 ;
            
$this -> prev_page_url  =   '' ;
        }

        
$page_list_ary   =   array ();
        
for  ( $t   =   $start $t   <=   $end $t ++ ) {
            
$page_list_ary []  =  ( $current_page == $t ?   ' <font color="red"><b> '   .   $t   .   ' </b></font> '   :   ' <a href=" '   .   $this -> file   .   ' ? '   .   $this -> page_var  .   ' = '   .   $t   .  ( $this -> var_str)  .   ' " target="_self"> '   .   $t   .   ' </a> ' ;
        }
        
$this -> output  .=   implode ( ' &nbsp;|&nbsp; ' ,   $page_list_ary );

        
if  ( $current_page   !=   $total_pages ) {
            
$this -> next_page      =   $current_page   +   1 ;
            
$this -> next_page_url  =   $this -> file   .   ' ? '   .   $this -> page_var  .   ' = '   .   $this -> next_page  .  ( $this -> var_str);
        } 
elseif  ( $current_page   =   $total_pages ) {
            
$this -> next_page      =   0 ;
            
$this -> next_page_url  =   '' ;
        }

        
// select下拉框直接跳转
         $this -> output  .=   " &nbsp;&nbsp;转到第<select οnchange="location=' "   .   $this -> file   .   ' ? '   .   $this -> page_var  .   " ='+this.options[this.selectedIndex].value+' "   .  ( $this -> var_str)  .   " '" align=absmiddle  style="font-size:8pt;border: 1px solid #999999;"> " ;
        
$show_all      =   200 ;
        
$slice_start   =   5 ;
        
$slice_end     =   5 ;
        
$percent       =   20 ;
        
$range         =   10 ;
        
if  ( $total_pages   <   $show_all ) {
            
$pages   =   range ( 1 ,   $total_pages );
        } 
else  {
            
$pages   =   array ();

            
//  Always show first X pages
             for  ( $i   =   1 $i   <=   $slice_start $i ++ ) {
                
$pages []  =   $i ;
            }

            
//  Always show last X pages
             for  ( $i   =   $total_pages   -   $slice_end $i   <=   $total_pages $i ++ ) {
                
$pages []  =   $i ;
            }

            
//  garvin: Based on the number of results we add the specified $percent percentate to each page number,
            // so that we have a representing page number every now and then to immideately jump to specific pages.
            // As soon as we get near our currently chosen page ($pageNow - $range), every page number will be
            // shown.

             $i   =   $slice_start ;
            
$x   =   $total_pages   -   $slice_end ;
            
$met_boundary   =   false ;
            
while ( $i   <=   $x ) {
                
if  ( $i   >=  ( $current_page   -   $range &&   $i   <=  ( $current_page   +   $range )) {
                    
//  If our pageselector comes near the current page, we use 1 counter increments
                     $i ++ ;
                    
$met_boundary   =   true ;
                } 
else  {
                    
//  We add the percentate increment to our current page to hop to the next one in range
                     $i   =   $i   +   floor ( $total_pages   /   $percent );

                    
//  Make sure that we do not cross our boundaries.
                     if  ( $i   >  ( $current_page   -   $range &&   ! $met_boundary ) {
                        
$i   =   $current_page   -   $range ;
                    }
                }

                
if  ( $i   >   0   &&   $i   <=   $x ) {
                    
$pages []  =   $i ;
                }
            }

            
//  Since because of ellipsing of the current page some numbers may be double,
            // we unify our array:

             sort ( $pages );
            
$pages   =   array_unique ( $pages );
        }
        
$this -> output  .=   ' <option value=" '   .   $current_page   .   ' "> '   .   $current_page   .   ' </option> '   .  NL;
        
foreach  ( $pages   AS   $i ) {
            
if  ( $i   ==   $current_page ) {
                
$selected   =   ' selected="selected" style="font-weight: bold" ' ;
            } 
else  {
                
$selected   =   '' ;
            }
            
$this -> output  .=   ' <option  '   .   $selected   .   '  value=" '   .   $i   .   ' "> '   .   $i   .   " </option> " ;
        }

        
$this -> output  .=   '  </select>页 ' ;


        
// 显示每页记录数及记录总数
         $this -> output  =   ' <span id=page_string>共 '   .   $record_nums   .   $unit   .   ' /本页[ '   .   $current_page   .   ' / '   .   $total_pages   .   ' '   .   $this -> output  .   ' </span> ' ;

    }
    
function  getPageData()
    {
        
reset ( $this -> data_ary);
        
krsort ( $this -> data_ary);
        
$i   =   0 ;
        
$ary   =   array ();
        
while  ( list ( $key ,   $val =   each ( $this -> data_ary)) {
            
if  ( $i   >=   $this -> offset  &&   $i   <  ( $this -> offset  +   $this -> num_per_page)) {
                
$ary [ $key =   $val ;
            }
            
$i ++ ;
        }
        
return   $ary ;
    }

    
function  setNumPerPage( $num_per_page )
    {
        
$this -> num_per_page  =   intval ( $num_per_page );
    }

    
/* *
     * 要传递的变量设置
     *
     * @access public
     * @param array $data   要传递的变量,用数组来表示,参见上面的例子
     * @return void
     
*/     
    
function  setVar( $data ,   $only_url   =   false ) {
        
if  ( is_array ( $data &&   count ( $data >   0 ) {
            
foreach  ( $data   as   $k   =>   $v ) {
                
$this -> var_str   .=   ' &amp; '   .   $k   .   ' = '   .   urlencode ( $v );
                
if  ( ! $only_url ) {
                    
$this -> data_ary   =   $this -> getColumnAry( $this -> data_ary ,   $k ,   $v );
                }
            }
        }
    }
    
/* *
     * 页面的接收变量名设置,page=3, p=3
     *
     * @access public
     * @param string $page_var 要传递的变量,如page
     * @return void
     
*/     
    
function  setPageVar( $page_var ) {
        
$this -> page_var  =   $page_var ;
    }
    
function  setLinkNum( $number )
    {
        
$this -> link_num  =   intval ( $number );
    }

    
function  getColumnAry( $data_ary ,   $column_name ,   $column_value )
    {
        
reset ( $data_ary );
        
$ary   =   array ();
        
while  ( list ( $key ,   $val =   each ( $data_ary )) {
            
if  ( array_key_exists ( $column_name ,   $val &&   $val [ $column_name ==   $column_value ) {
                
$ary [ $key =   $val ;
            }
        }
        
return   $ary ;
    }


    
/* *
     * 分页结果输出
     *
     * @access public
     * @param bool $return 为真时返回一个字符串,否则直接输出,默认直接输出
     * @return string
     
*/
    
function  output( $return   =   false ) {
        
if  ( $return ) {
            
return   $this -> output;
        } 
else  {
            
echo   $this -> output;
        }
    }
    
function  RecordCount()
    {
        
$this -> record_nums  =   count ( $this -> data_ary);
        
return   $this -> record_nums;
    }

    
function  getOffset()
    {
        
return   $this -> offset;
    }
    
function  getTotalPages()
    {
        
return   $this -> total_pages;
    }
    
function  getCurrentPage()
    {
        
return   $this -> current_page;
    }
    
function  getNumPerPage()
    {
        
return   $this -> num_per_page;
    }

// End Class
?>
    
function  getNumPerPage()
    {
        
return   $this -> num_per_page;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值