zoukankan      html  css  js  c++  java
  • 根据网上代码 自己改的PHP分页类,不含ajax无刷新分页

    PHP 分页类叫 page.class.php

      1 <?php
      2 class pageTest
      3 {
      4     public   $first_row;           //起始行数
      5 
      6     public   $list_rows;        //列表每页显示行数
      7     
      8     public   $total_pages;      //总页数
      9 
     10     public   $total_rows;          //总行数
     11     
     12     public   $now_page;         //当前页数
     13     
     14     public   $method  = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式 
     15     
     16     public   $parameter = '';
     17     
     18     public   $page_name;        //分页参数的名称
     19     
     20     //public    $ajax_func_name;
     21     
     22     public        $plus = 3;         //分页偏移量
     23     
     24     public  $url;
     25     
     26      /**
     27      * 构造函数
     28      * @param unknown_type $data
     29      */
     30      
     31      public function __construct($data = array()){
     32          
     33         $this->total_rows = $data['total_rows']; //总行数
     34         
     35         $this->list_rows         = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
     36                                         //每页显示行数 默认为15行
     37         $this->total_pages        = ceil($this->total_rows / $this->list_rows);
     38                                         //总页数
     39         $this->parameter         = !empty($data['parameter']) ? $data['parameter'] : '';  //传递过来的参数
     40         
     41         $this->page_name          = !empty($data['page_name']) ? $data['page_name'] : 'p';  //传递参数的命名
     42                                                                                       
     43         $this->method           = !empty($data['method']) ? $data['method'] : '';
     44          
     45          //当前页面
     46          if(!empty($data['now_page'])){
     47              
     48              $this->now_page = intval($data['now_page']);
     49              
     50          }else{
     51              
     52              $this->now_page = !empty($_GET[$this->page_name]) ? $_GET[$this->page_name] : 1;
     53              
     54          }
     55          
     56              $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;
     57           
     58          if(!empty($this->total_pages) && ($this->now_page > $this->total_pages)){
     59               
     60              $this->now_page = $this->total_pages;
     61             
     62          }
     63          
     64              $this->first_row = $this->list_rows * ($this->now_page-1);  
     65              
     66      }
     67     /* 测试功能的函数*/
     68     public function aa(){
     69     
     70     echo $_GET[$this->page_name];
     71          
     72     }
     73     public function _get_link($page,$text){
     74         
     75         return '<a href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";
     76         
     77     }
     78     
     79       public function _set_url()
     80     {
     81         $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
     82         
     83         $parse = parse_url($url);
     84         
     85         if(isset($parse['query'])) {
     86             parse_str($parse['query'],$params);
     87             
     88             unset($params[$this->page_name]);
     89             
     90             $url   =  $parse['path'].'?'.http_build_query($params);
     91             
     92         }
     93         if(!empty($params))
     94         {
     95             $url .= '&';
     96         }
     97         $this->url = $url;
     98         
     99 
    100     }
    101      public function _get_url($page)
    102     {
    103         if($this->url === NULL)
    104         {
    105             $this->_set_url();    
    106         }
    107       //    $lable = strpos('&', $this->url) === FALSE ? '' : '&';
    108         return $this->url . $this->page_name . '=' . $page;
    109     }
    110     
    111       /**
    112      * 得到第一页
    113      * @return string
    114      */
    115     public function first_page($name = '第一页')
    116     {
    117          if($this->now_page > 5)
    118          {
    119              return $this->_get_link('1', $name);
    120          }    
    121          return '';
    122     }
    123     
    124     /**
    125      * 最后一页
    126      * @param $name
    127      * @return string
    128      */
    129     public function last_page($name = '最后一页')
    130     {
    131          if($this->now_page < $this->total_pages - 5)
    132          {
    133              return $this->_get_link($this->total_pages, $name);
    134          }    
    135          return '';
    136     }  
    137     
    138     /**
    139      * 上一页
    140      * @return string
    141      */
    142     public function up_page($name = '上一页')
    143     {
    144         if($this->now_page != 1)
    145         {
    146             return $this->_get_link($this->now_page - 1, $name);
    147         }
    148         return '';
    149     }
    150     
    151     /**
    152      * 下一页
    153      * @return string
    154      */
    155     public function down_page($name = '下一页')
    156     {
    157         if($this->now_page < $this->total_pages)
    158         {
    159             return $this->_get_link($this->now_page + 1, $name);
    160         }
    161         return '';
    162     }
    163     
    164      public function show($param = 1)
    165     {
    166         if($this->total_rows < 1)
    167         {
    168             return '';
    169         }
    170         
    171         $className = 'show_' . $param;
    172         
    173         $classNames = get_class_methods($this);
    174         
    175         if(in_array($className, $classNames))
    176         {
    177             return $this->$className();
    178         }
    179           return '';
    180     }
    181     
    182         public function show_1()
    183     {
    184         $plus = $this->plus;
    185         if( $plus + $this->now_page > $this->total_pages)
    186         {
    187             $begin = $this->total_pages - $plus * 2;
    188         }else{
    189             $begin = $this->now_page - $plus;
    190         }
    191         
    192         $begin = ($begin >= 1) ? $begin : 1;
    193         $return = '';
    194         $return .= $this->first_page();
    195         $return .= $this->up_page();
    196         for ($i = $begin; $i <= $begin + $plus * 2;$i++)
    197         {
    198             if($i>$this->total_pages)
    199             {
    200                 break;
    201             }
    202             if($i == $this->now_page)
    203             {
    204                 $return .= "<a class='now_page'>$i</a>\n";
    205             }
    206             else
    207             {
    208                 $return .= $this->_get_link($i, $i) . "\n";
    209             }
    210         }
    211         $return .= $this->down_page();
    212         $return .= $this->last_page();
    213         return $return;
    214     }
    215 }
    216 ?>

    使用方法 我介绍的是在.php文件中的使用方法,因为本人本地 没有用html 与PHP 分离

    代码是:

    /*首先肯定是连接数据库,数据库类的代码我就不发了。*/
    
    require_once("Page.class.php");
          $tid='111';
           $params = array(
                'total_rows'=>10, #(必须)
                'method'    =>'', #(必须)
                'parameter' =>'tid='.$tid,  #(看情况,这个是传输的参数)
                'now_page'  =>$_GET['p'],  #(必须)
                'list_rows' =>2, #(可选) 默认为15
            );
    
    $page = new pageTest($params);
    
    $sql  = "select title from dede_archives where 1 limit ".$page->first_row.",".$page->list_rows."";
    
    $query = mysql_query($sql);
    
    $arr1 = array();
    while($arr = mysql_fetch_array($query)){
        
        echo  "<br>".$arr[title]."<br>";
    }
    
    ?>
    <div id="page">
        <?php echo $page->show(1);
        echo "<br>";
        
        ?></div>
    <style type="text/css">
    #page{font:12px/16px arial}
    #page span{float:left;margin:0px 3px;}
    #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}
    #page a.now_page,#page a:hover{color:#fff;background:#05c}
    </style>
    
    //style 的是分页样式
    //PHP 部分是 使用方法
    //html 部分是 显示时用到的DIV
  • 相关阅读:
    Vue 报错[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders
    常用的英语词汇
    增加个人形象气质
    网络基础
    MK-编辑器
    Nexus仓库构建
    POM的配置文件
    Dubbo_Admin安装
    dubbo管理控制台安装和使用
    Dubbox监控在服务器中的安装
  • 原文地址:https://www.cnblogs.com/ymj0906/p/3020592.html
Copyright © 2011-2022 走看看