ThinkPHP实现分页功能

时间:2022-10-26 11:57:22

前几篇(上传,缩略图,验证码,自动验证表单)文章介绍的功能实现都是基于ThinkPHP框架封装好的类进行实现的,所以这次自己写一个分页类在框架中使用。

首先在根目录建一个Tools文件夹,在Tools文件夹下建Page.class.php类文件,这样以后自定义的工具类都可放在Tools文件夹下。

此类封装有以下函数:获取请求地址,开始页,从哪一条显示,结束页 从哪一条结束,页码列表(首页超链接,上一页,页码数字列表超链接,下一页,尾页,跳转),对于分页足够使用!

下面是Page.class.php代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
//命名空间的名称与上级目录tools一致
//原因:当前Page.class.php类文件会被自动加载机制引入
//   在引入的同时会把"tools"变为文件的上级目录,进而获得该Page类文件
namespace Tools;
 
class Page {
  private $total; //数据表中总记录数
  private $listRows; //每页显示行数
  private $limit;
  private $uri//当前链接URL
  private $pageNum; //页数
  private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页");
  private $listNum=8; //限制页码列表数目
  
  /*
   * $total 总记录数
   * $listRows 每页显示行数
   */
  public function __construct($total, $listRows=10, $pa=""){
    $this->total=$total//数据表中总记录数
    $this->listRows=$listRows; //设置每页显示行数
    $this->uri=$this->getUri($pa); //请求地址
    $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; //当前页
    $this->pageNum=ceil($this->total/$this->listRows); //总页数
    $this->limit=$this->setLimit(); //限制每页长度
  }
 
  private function setLimit(){
    return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
  }
 
  //请求地址
  private function getUri($pa){
    $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;
    $parse=parse_url($url);
 
  
 
    if(isset($parse["query"])){
      parse_str($parse['query'],$params);
      unset($params["page"]);
      $url=$parse['path'].'?'.http_build_query($params);
      
    }
 
    return $url;
  }
 
  function __get($args){
    if($args=="limit")
      return $this->limit;
    else
      return null;
  }
 
  //开始页,从哪一条显示
  private function start(){
    if($this->total==0)
      return 0;
    else
      return ($this->page-1)*$this->listRows+1;
  }
 
  //从哪一条结束
  private function end(){
    return min($this->page*$this->listRows,$this->total);
  }
 
  //首页超链接
  private function first(){
    $html = "";
    if($this->page==1)
      $html.='';
    else
      $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>&nbsp;&nbsp;";
 
    return $html;
  }
 
  //上一页
  private function prev(){
    $html = "";
    if($this->page==1)
      $html.='';
    else
      $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;&nbsp;";
 
    return $html;
  }
 
  //页码数字列表超链接
  private function pageList(){
    $linkPage="";
    
    $inum=floor($this->listNum/2);
  
    for($i=$inum; $i>=1; $i--){
      $page=$this->page-$i;
 
      if($page<1)
        continue;
 
      $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
 
    }
  
    $linkPage.="&nbsp;{$this->page}&nbsp;";
    
 
    for($i=1; $i<=$inum; $i++){
      $page=$this->page+$i;
      if($page<=$this->pageNum)
        $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
      else
        break;
    }
 
    return $linkPage;
  }
 
  //下一页
  private function next(){
    $html = "";
    if($this->page==$this->pageNum)
      $html.='';
    else
      $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;&nbsp;";
 
    return $html;
  }
 
  //尾页
  private function last(){
    $html = "";
    if($this->page==$this->pageNum)
      $html.='';
    else
      $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;&nbsp;";
 
    return $html;
  }
 
  //跳转
  private function goPage(){
    return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'">&nbsp;&nbsp;';
  }
 
  //页码列表
  function fpage($display=array(0,1,2,3,4,5,6,7,8)){
    $html[0]="&nbsp;&nbsp;共有<b>{$this->total}</b>{$this->config["header"]}&nbsp;&nbsp;";
    $html[1]="&nbsp;&nbsp;每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条&nbsp;&nbsp;";
    $html[2]="&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;&nbsp;";
    
    $html[3]=$this->first();
    $html[4]=$this->prev();
    $html[5]=$this->pageList();
    $html[6]=$this->next();
    $html[7]=$this->last();
    $html[8]=$this->goPage();
    $fpage='';
    foreach($display as $index){
      $fpage.=$html[$index];
    }
 
    return $fpage;
 
  }
 
 
}

控制器代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//商品列表
  function showlist(){
 
   //实现分页效果
   $goods = D('goods');
 
   //① 获得数据的总记录条数
   $total = $goods -> count(); //select count(*) from sw_goods;
   $per = 7; //每页显示7条记录
 
   //②实例化分页类
   $page_obj = new \Tools\Page($total, $per);
 
   //③自定义sql语句,获取每页信息
   $sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit;
   $info = $goods->query($sql);
 
   //④获取页码列表
   $pagelist = $page_obj->fpage(array(3,4,5,6,7,8));
 
   //分配
   $this->assign('pagelist',$pagelist);
   $this->assign('info',$info);
   $this->display();
  }

前台显示代码

<{$pagelist}>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。