• php一个不错的分页


    1.分页源码

      1 <?php
      2 class PageView{
      3     /**页码**/
      4     public $pageNo = 1;
      5     /**页大小**/
      6     public $pageSize = 20;
      7     /**共多少页**/
      8     public $pageCount = 0;
      9     /**总记录数**/
     10     public $totalNum = 0;
     11     /**偏移量,当前页起始行**/
     12     public $offSet = 0;
     13     /**每页数据**/
     14     public $pageData = array();
     15     
     16     /**是否有上一页**/
     17     public $hasPrePage = true;
     18     /**是否有下一页**/
     19     public $hasNextPage = true;
     20     
     21     public $pageNoList = array();
     22     
     23     public $jsFunction ='jsFunction';
     24     /**
     25      * 
     26      * @param unknown_type $count 总行数
     27      * @param unknown_type $size 分页大小
     28      * @param unknown_type $string
     29      */
     30     public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
     31 
     32         $this->totalNum = $count;//总记录数
     33         $this->pageSize = $size;//每页大小
     34         $this->pageNo = $pageNo;
     35         //计算总页数
     36         $this->pageCount = ceil($this->totalNum/$this->pageSize);
     37         $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
     38         //检查pageNo
     39         $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
     40         $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;
     41         
     42         //计算偏移
     43         $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
     44         //计算是否有上一页下一页
     45         $this->hasPrePage = $this->pageNo == 1 ?false:true; 
     46 
     47         $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;
     48         
     49         $this->pageData = $pageData;
     50         $this->jsFunction = $jsFunction;
     51         
     52     }
     53     /**
     54      * 分页算法
     55      * @return
     56      */
     57     private function generatePageList(){
     58         $pageList = array();
     59         if($this->pageCount <= 9){
     60             for($i=0;$i<$this->pageCount;$i++){
     61                 array_push($pageList,$i+1);
     62             }
     63         }else{
     64             if($this->pageNo <= 4){
     65                 for($i=0;$i<5;$i++){
     66                     array_push($pageList,$i+1);
     67                 }
     68                 array_push($pageList,-1);
     69                 array_push($pageList,$this->pageCount);
     70 
     71             }else if($this->pageNo > $this->pageCount - 4){
     72                 array_push($pageList,1);
     73                 
     74                 array_push($pageList,-1);
     75                 for($i=5;$i>0;$i--){
     76                     array_push($pageList,$this->pageCount - $i+1);
     77                 }
     78             }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
     79                 array_push($pageList,1);
     80                 array_push($pageList,-1);
     81                 
     82                 array_push($pageList,$this->pageNo -2);
     83                 array_push($pageList,$this->pageNo -1);
     84                 array_push($pageList,$this->pageNo);
     85                 array_push($pageList,$this->pageNo + 1);
     86                 array_push($pageList,$this->pageNo + 2);
     87                 
     88                 array_push($pageList,-1);
     89                 array_push($pageList,$this->pageCount);
     90                 
     91             }
     92         }
     93         return $pageList;
     94     }
     95 
     96     /***
     97      * 创建分页控件
     98     * @param
     99     * @return String
    100     */
    101     public function echoPageAsDiv(){
    102         $pageList = $this->generatePageList();
    103         
    104         $pageString ="<div class='pagination'><div class='page-bottom'>";
    105     
    106         if(!empty($pageList)){
    107             if($this->pageCount >1){
    108                 if($this->hasPrePage){
    109                     $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";
    110                 }
    111                 foreach ($pageList as $k=>$p){
    112                     if($this->pageNo == $p){
    113                         $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
    114                         continue;
    115                     }
    116                     if($p == -1){
    117                         $pageString = $pageString ."<span class='page-break'>...</span>";
    118                         continue;
    119                     }
    120                     $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
    121                 }
    122                 
    123                 if($this->hasNextPage){
    124                     $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";
    125                 }
    126                 
    127             }
    128         }
    129         $pageString = $pageString .("</div></div>");
    130         return $pageString;
    131     }
    132 }
    133 
    134 ?>

    2.php调用

     1    $pageNo = $_GET['pageNo'];
     2         if(empty($pageNo)){
     3             $pageNo = 1;
     4         }
     5         //分页数据
     6         $pageData = News::getNewsPage($pageNo,$pageSize);
     7        //取得总行数
     8         $count = News::getNewsCount();
     9         //创建分页器
    10         $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
    11      //生成页码
    12         $pageViewString = $p->echoPageAsDiv();

    3.效果图:

  • 相关阅读:
    Codeforces Round #538 (Div. 2) F. Please, another Queries on Array?
    2021 ICPC济南 J Determinant
    牛客小白月赛43 F 全体集合
    The 2021 ICPC Asia Regionals Online Contest (II) L Euler Function
    C++文件操作详解
    利用VC实现图像的特殊显示效果 小楼machine
    Mixed mode assembly is built against version 'v2.0.50727' 解决方案 小楼machine
    如何在对话框资源从一个项目导入到另一个项目使用 Visual c + +.net 或 Visual c + + 2005 小楼machine
    灰度图像阈值化分割常见方法总结及VC实现 小楼machine
    Visual C++多媒体设计及图形、图像处理 小楼machine
  • 原文地址:https://www.cnblogs.com/nghygaojun/p/3280599.html
Copyright © 2020-2023  润新知