PHP分页类

View Code
  1 <?php
  2 /**
  3  * datagrid by asmfang Q184377367
  4  *
  5  */
  6 class datagrid{
  7     
  8     //每页显示数量
  9     public  $evetotal        = 0;
 10     //数据总数
 11     public  $datacount        = 0;
 12     //总页数
 13     public  $pagetotal         = 0;
 14     //当前页码
 15     public  $currentpage    = 0;
 16     
 17     /**
 18         array = (url,target);
 19      */
 20     public  $urlroute     = array();
 21     
 22     //数据结果集
 23     public $res = array();
 24     
 25 
 26     /**
 27      * 初始化动作,通过外部回调函数获取数据
 28      *
 29      * @param object $dbobj          数据对象
 30      * @param string $callback        数据对象回调方法
 31      * @param string $datastr        SQL连接字符
 32      * @param int $datacount        数据总数
 33      * @param int $_evetotal        每页显示数量
 34      * @param string $url            URL连接
 35      */
 36     public function init_data(     $dbobj,
 37                                 $callback, 
 38                                 $datastr, 
 39                                 $datacount, 
 40                                 $_evetotal
 41                                 ) {
 42         
 43         $this->evetotal     = $_evetotal;                                     //每页显示
 44         $this->datacount     = $datacount;                                     //数据总数
 45         $this->pagetotal    = ceil( $this->datacount / $this->evetotal );     //总页数
 46         $this->currentpage     =  ( $this->currentpage <= 0x00000000 )         ? 1 : $this->currentpage ;
 47         $this->currentpage     =  ( $this->currentpage > $this->pagetotal )     ? $this->pagetotal : $this->currentpage;
 48         $offset                =  ($this->currentpage - 1) * $this->evetotal;
 49         $datastr            .=  ' LIMIT '. $offset .','.$this->evetotal;
 50         $this->res           =  $dbobj->$callback( $datastr );
 51 
 52     }
 53     
 54     public function init_page_parem( $url = '', $tag = 'self', $sufix = 'p', $cpage = 0, $gpage = true ){
 55         $this->urlroute['gpage']     = $gpage;
 56         $this->urlroute['url']         = $url;
 57         $this->urlroute['tag']         = $tag;
 58         $this->urlroute['sufix']     = $sufix;
 59         $this->urlroute['cpage']     = $cpage;
 60         if ( $this->urlroute['gpage'] ) {
 61              $this->currentpage     =  isset( $_GET[$this->urlroute['sufix']] ) ? (int)( $_GET[$this->urlroute['sufix']] ) : 1;
 62              
 63         } else {
 64             $this->currentpage = $this->urlroute['cpage'];
 65         }
 66         
 67     }
 68     
 69     public function show(){
 70         
 71         $this->show_datagrid();
 72         $mpurl = strpos( $this->urlroute['url'], '?' ) ? '&' : '?';
 73         $html  = '<table><tr>';
 74         for ( $i=1; $i <= $this->pagetotal; $i++ ) {
 75             $html.= '<td><a href=' .$this->urlroute['url'].$mpurl. 
 76                                     $this->urlroute['sufix'].'='.$i.' target='. 
 77                                     $this->urlroute['tag'] .'>[ ' .$i. ' ]</a></td>';
 78         }
 79         $html.= '</tr></table>';
 80         echo $html;
 81         unset( $mpurl,$html );
 82     }
 83     
 84 
 85     private function show_datagrid() {
 86         $html = '<table style=\'border: solid 1px #000000;width:900px;border-collapse: collapse;\'>';
 87         foreach ( $this->res as $key => $val) {
 88             
 89             $html .= '<tr>';
 90             foreach ( $val as $keya => $vala ) {
 91                 
 92                 $html .= '<td style=\'border:1px #000000 solid;\'>';
 93                 $html .=  $vala;
 94                 $html .= '</td>';
 95                 
 96             }
 97             $html .= '</tr>';
 98             
 99         }
100         $html .= '</table>';
101         echo $html;
102     }
103     
104 
105     
106 }
107 
108 
109 ?>

 

 

posted @ 2012-05-09 13:09  方东信  阅读(198)  评论(0编辑  收藏  举报