分享一个很不错的PHP分页类

0

page

首先声明这个类不是朽木原创的,没找到出处,今天用到了 顺手码了一段CSS样式.

使用方法很简单只需要指定总页数就可以了.

$pager = new page($total);
echo $pager->pageShow();

数据库操作

//总条数
$total = $db->getVar("SELECT count(*) FROM `{$table}`");
//初始化分页类 
$pager = new page($total);
$sql = "SELECT * FROM `{$table}` ORDER BY `id` DESC LIMIT ".$pager->__get('limit');
$list = $db->getData($sql);
//分页HTML
$pagehtml = $pager->pageShow();

page.css

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
/*page*/
.page{
  margin-top: 20px;
  padding: 0 30px;
}
.page span,.page a{
  display: inline-block;
  border:1px solid #ccc;
  padding:5px 7px 4px 7px;
  margin-right: 5px;
  background: #efefef;
  color: #000;
}
.page a:hover{
  background-color:#e0e0e0;
	-o-transition: background-color .20s linear;
	-webkit-transition: background-color .20s linear;
	-moz-transition: background-color .20s linear;
	transition:  background-color .20s linear;
}
.page .pageher{
  display: block;
  width: 150px;
  float: right;
  text-align: right;
  background: none;
  border: none;
  cursor: default;
}
.page .disabled{
  color:#999;
  cursor: default;
}
.page .current{
  color:#fff;
  background: #0086D2;
  border-color: #0086D2;
  cursor: default;
}

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
class Page {
	private $total;			//总数量
	private $limit;			//返回mysql的limit语句
	private $pageStart;		//开始的数值
	private $pageStop;		//结束的数值
	private $pageNumber;	//显示分页数字的数量
	private $page;			//当前页
	private $pageSize;		//每页显示的数量
	private $pageToatl;		//分页的总数量
	private $pageParam;		//分页变量
	private $uri;			//URL参数
	/**
	 * 分页设置样式 不区分大小写
	 * %pageToatl%  //总页数
	 * %page%		//当前页
	 * %pageSize% 	//当前页显示数据条数
	 * %pageStart%	//本页起始条数
	 * %pageStop%	//本页结束条数
	 * %total%		//总数据条数
	 * %first%		//首页
	 * %ending%		//尾页
	 * %up%			//上一页
	 * %down%		//下一页
	 * %F%			//起始页
	 * %E%			//结束页
	 * %omitFA%		//前省略加跳转
	 * %omitEA%		//后省略加跳转
	 * %omitF%		//前省略
	 * %omitE%		//后省略
	 * %numberF%	//固定数量的数字分页
	 * %numberD%	//左右对等的数字分页
	 * %input%		//跳转输入框
	 * %GoTo%			//跳转按钮
	 */
	private $pageType = '<span class="pageher">第%page%页/共%pageToatl%页</span>%first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
	//显示值设置
	private $pageShow = array('first'=>'首页','ending'=>'尾页','up'=>'上一页','down'=>'下一页','GoTo'=>'GO');
 
	/**
	 * 初始化数据,构造方法
	 * @access public
	 * @param int $total 		数据总数量
	 * @param int $pageSize 	每页显示条数
	 * @param int $pageNumber 	分页数字显示数量(使用%numberF%和使用%numberD%会有不同的效果)
	 * @param string $pageParam	分页变量
	 * @return void
	 */
	public function __construct($total,$pageSize=10,$pageNumber=5,$pageParam='p'){
		$this->total = $total<0?0:$total;
		$this->pageSize = $pageSize<0?0:$pageSize;
		$this->pageNumber = $pageNumber<0?0:$pageNumber;
		$this->pageParam = $pageParam;
		$this->calculate();
	}
 
	/**
	 * 显示分页
	 * @access public 
	 * @return string HTML分页字符串
	 */
	public function pageShow(){
		$this->uri();
		if($this->pageToatl>1){
			if($this->page == 1){
				$first = '<span class="disabled">'.$this->pageShow['first'].'</span>';
				$up = '<span class="disabled">'.$this->pageShow['up'].'</span>';
			}else{
				$first = '<a href="'.$this->uri.'&'.$this->pageParam.'=1">'.$this->pageShow['first'].'</a>';
				$up = '<a href="'.$this->uri.'&'.$this->pageParam.'='.($this->page-1).'">'.$this->pageShow['up'].'</a>';
			}
			if($this->page >= $this->pageToatl){
				$ending = '<span class="disabled">'.$this->pageShow['ending'].'</span>';
				$down = '<span class="disabled">'.$this->pageShow['down'].'</span>';
			}else{
				$ending = '<a href="'.$this->uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageShow['ending'].'</a>';
				$down = '<a href="'.$this->uri.'&'.$this->pageParam.'='.($this->page+1).'">'.$this->pageShow['down'].'</a>';
			}
			$input  = '<input type="text" value="'.$this->page.'" onkeydown="javascript: if(event.keyCode==13){var value = parseInt(this.value); var page=(value>'.$this->pageToatl.') ? '.$this->pageToatl.' : value;  location=\''.$this->uri.'&'.$this->pageParam.'=\'+page+\'\'; return false;}">';
			$GoTo = '<button onclick="javascript:var value=parseInt(this.previousSibling.value); var page=(value>'.$this->pageToatl.') ? '.$this->pageToatl.' : value;  location=\''.$this->uri.'&'.$this->pageParam.'=\'+page+\'\'; return false;">'.$this->pageShow['GoTo'].'</button>';
		}else{
			$first = '';$up ='';$ending = '';$down = '';$input = '';$GoTo = '';
		}
		$this->numberF();
		return str_ireplace(array('%pageToatl%','%page%','%pageSize%','%pageStart%','%pageStop%','%total%','%first%','%ending%','%up%','%down%','%input%','%GoTo%'),array($this->pageToatl,$this->page,$this->pageStop-$this->pageStart,$this->pageStart,$this->pageStop,$this->total,$first,$ending,$up,$down,$input,$GoTo),$this->pageType);
	}
 
 
	/**
	 *	数字分页
	 */
	private function numberF(){
		$pageF = stripos($this->pageType,'%numberF%');
		$pageD = stripos($this->pageType,'%numberD%');
		$numberF = '';$numberD = '';$F = '';$E ='';$omitF = '';$omitFA = '';$omitE = '';$omitEA = '';
		if($pageF!==false || $pageD!==false){
			if($pageF!==false){
				$number = $this->pageNumber%2==0?$this->pageNumber/2:($this->pageNumber+1)/2;
				$DStart = $this->page - $number<0?$this->page - $number-1:0;
				if($this->page+$number-$DStart > $this->pageToatl){
					$DStop = ($this->page+$number-$DStart) - $this->pageToatl;
					$forStop = $this->pageToatl+1;
				}else{
					$DStop = 0;
					$forStop = $this->page+$number-$DStart+1;
				}
				$forStart = $this->page-$number-$DStop<1?1:$this->page-$number-$DStop;
				for($i=$forStart;$i<$forStop;++$i){
					if($i==$this->page){
						$numberF .= '<span class="current">'.$i.'</span>';
					}else{
						$numberF .= '<a href="'.$this->uri.'&'.$this->pageParam.'='.$i.'">'.$i.'</a>';
					}
				}
			}
			if($pageD!==false){
				$number = $this->pageNumber;
				$forStart = $this->page-$number>0?$this->page-$number:1;
				$forStop = $this->page+$number>$this->pageToatl?$this->pageToatl+1:$this->page+$number+1;
				for($i=$forStart;$i<$this->page;++$i){
					$numberD .= '<a href="'.$this->uri.'&'.$this->pageParam.'='.$i.'">'.$i.'</a>';
				}
				$numberD .= '<span class="current">'.$this->page.'</span>';
				$start = $this->page+1;
				for($i=$start;$i<$forStop;++$i){
					$numberD .= '<a href="'.$this->uri.'&'.$this->pageParam.'='.$i.'">'.$i.'</a>';
				}
			}
			$F = $forStart>1?'<a href="'.$this->uri.'&'.$this->pageParam.'=1">1</a>':'';
			$E = $forStop<$this->pageToatl+1?'<a href="'.$this->uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageToatl.'</a>':'';
			if($forStart>2){
				$omitF = '<span  class="omit">…</span>';
				$startA = $this->page-$number<1?1:$this->page-$number;
				$omitFA = '<a  class="omit" href="'.$this->uri.'&'.$this->pageParam.'='.$startA.'">…</a>';
			}
			if($forStop<$this->pageToatl){
				$omitE = '<span class="omit">…</span>';
				$stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
				$omitEA = '<a  class="omit" href="'.$this->uri.'&'.$this->pageParam.'='.$stopA.'">…</a>';
			}
		}
		$this->pageType = str_ireplace(array('%F%','%E%','%omitFA%','%omitEA%','%omitF%','%omitE%','%numberF%','%numberD%'),array($F,$E,$omitFA,$omitEA,$omitF,$omitE,$numberF,$numberD),$this->pageType);
	}
 
	/**
	 *	处理url的方法
	 * @access public
	 * @param array   $url 	保持URL直对关系数组
	 * @return string 		过滤后的url尾参数
	 */
	private function uri(){
		$url = $_SERVER["REQUEST_URI"];
		$par = parse_url($url);
		if (isset($par['query'])) {
			parse_str($par['query'],$query);
			if(!is_array($this->uri)){
				$this->uri = array();
			}
			array_merge($query,$this->uri);
			unset($query[$this->pageParam]);
			while($key = array_search('',$query)){
				unset($query[$key]);
			}
			$this->uri = $par['path'].'?'.http_build_query($query);
		}else{
			$this->uri = $par['path'].'?';
		}
	}
 
	/**
	 * 设置limit方法及计算起始条数和结束条数
	 */
	private function calculate(){
		$this->pageToatl = ceil($this->total/$this->pageSize);
		$this->page = intval($_GET[$this->pageParam]);
		$this->page = $this->page>=1?$this->page>$this->pageToatl?$this->pageToatl:$this->page:1;
		$this->pageStart = ($this->page-1)*$this->pageSize;
		$this->pageStop = $this->pageStart+$this->pageSize;
		$this->pageStop = $this->pageStop>$this->total?$this->total:$this->pageStop;
		$this->limit = $this->pageStart.','.$this->pageStop;
	}
 
	/**
	 * 设置过滤器
	 */
	public function __set($name,$value){
		switch($name){
			case 'pageType':
			case 'uri':
				$this->$name = $value;
				return;
			case 'pageShow':
				if(is_array($value)){
					$this->pageShow = array_merge($this->pageShow,$value);
				}
				return;
		}
	}
 
	/**
	 * 取值过滤器
	 */
	public function __get($name){
		switch($name){
			case 'limit':
			case 'pageStart':
			case 'pageStop':
				return $this->$name;
			default:
				return;
		}
	}
}

Bootstrap 适用

1
2
3
4
5
<ul class="pagination pagination-sm m-t-none m-b-none">
  <?php echo $pagehtml?>
</ul>
 
共 <?php echo $total ?> 条数据 当前显示第 <?php echo $pager->__get('pageStart') ?>-<?php echo $pager->__get('pageStop') ?> 条
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
class Page {
	private $total;			//总数量
	private $limit;			//返回mysql的limit语句
	private $pageStart;		//开始的数值
	private $pageStop;		//结束的数值
	private $pageNumber;	//显示分页数字的数量
	private $page;			//当前页
	private $pageSize;		//每页显示的数量
	private $pageToatl;		//分页的总数量
	private $pageParam;		//分页变量
	private $uri;			//URL参数
	/**
	 * 分页设置样式 不区分大小写
	 * %pageToatl%  //总页数
	 * %page%		//当前页
	 * %pageSize% 	//当前页显示数据条数
	 * %pageStart%	//本页起始条数
	 * %pageStop%	//本页结束条数
	 * %total%		//总数据条数
	 * %first%		//首页
	 * %ending%		//尾页
	 * %up%			//上一页
	 * %down%		//下一页
	 * %F%			//起始页
	 * %E%			//结束页
	 * %omitFA%		//前省略加跳转
	 * %omitEA%		//后省略加跳转
	 * %omitF%		//前省略
	 * %omitE%		//后省略
	 * %numberF%	//固定数量的数字分页
	 * %numberD%	//左右对等的数字分页
	 * %input%		//跳转输入框
	 * %GoTo%			//跳转按钮
	 */
	private $pageType = '%first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
	//显示值设置
	private $pageShow = array('first'=>'首页','ending'=>'尾页','up'=>'上一页','down'=>'下一页','GoTo'=>'GO');
 
	/**
	 * 初始化数据,构造方法
	 * @access public
	 * @param int $total 		数据总数量
	 * @param int $pageSize 	每页显示条数
	 * @param int $pageNumber 	分页数字显示数量(使用%numberF%和使用%numberD%会有不同的效果)
	 * @param string $pageParam	分页变量
	 * @return void
	 */
	public function __construct($total,$pageSize=10,$pageNumber=5,$pageParam='p'){
		$this->total = $total<0?0:$total;
		$this->pageSize = $pageSize<0?0:$pageSize;
		$this->pageNumber = $pageNumber<0?0:$pageNumber;
		$this->pageParam = $pageParam;
		$this->calculate();
	}
 
	/**
	 * 显示分页
	 * @access public 
	 * @return string HTML分页字符串
	 */
	public function pageShow(){
		$this->uri();
		if($this->pageToatl>1){
			if($this->page == 1){
				$first = '<li class="disabled"><span>'.$this->pageShow['first'].'</span></li>';
				$up = '<li class="disabled"><span>'.$this->pageShow['up'].'</span></li>';
			}else{
				$first = '<li><a href="'.$this->uri.'&'.$this->pageParam.'=1">'.$this->pageShow['first'].'</a></li>';
				$up = '<li><a href="'.$this->uri.'&'.$this->pageParam.'='.($this->page-1).'">'.$this->pageShow['up'].'</a></li>';
			}
			if($this->page >= $this->pageToatl){
				$ending = '<li class="disabled"><span>'.$this->pageShow['ending'].'</span></li>';
				$down = '<li class="disabled"><span>'.$this->pageShow['down'].'</span></li>';
			}else{
				$ending = '<li><a href="'.$this->uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageShow['ending'].'</a></li>';
				$down = '<li><a href="'.$this->uri.'&'.$this->pageParam.'='.($this->page+1).'">'.$this->pageShow['down'].'</a></li>';
			}
			$input  = '<input type="text" value="'.$this->page.'" onkeydown="javascript: if(event.keyCode==13){var value = parseInt(this.value); var page=(value>'.$this->pageToatl.') ? '.$this->pageToatl.' : value;  location=\''.$this->uri.'&'.$this->pageParam.'=\'+page+\'\'; return false;}">';
			$GoTo = '<button onclick="javascript:var value=parseInt(this.previousSibling.value); var page=(value>'.$this->pageToatl.') ? '.$this->pageToatl.' : value;  location=\''.$this->uri.'&'.$this->pageParam.'=\'+page+\'\'; return false;">'.$this->pageShow['GoTo'].'</button>';
		}else{
			$first = '';$up ='';$ending = '';$down = '';$input = '';$GoTo = '';
		}
		$this->numberF();
		return str_ireplace(array('%pageToatl%','%page%','%pageSize%','%pageStart%','%pageStop%','%total%','%first%','%ending%','%up%','%down%','%input%','%GoTo%'),array($this->pageToatl,$this->page,$this->pageStop-$this->pageStart,$this->pageStart,$this->pageStop,$this->total,$first,$ending,$up,$down,$input,$GoTo),$this->pageType);
	}
 
 
	/**
	 *	数字分页
	 */
	private function numberF(){
		$pageF = stripos($this->pageType,'%numberF%');
		$pageD = stripos($this->pageType,'%numberD%');
		$numberF = '';$numberD = '';$F = '';$E ='';$omitF = '';$omitFA = '';$omitE = '';$omitEA = '';
		if($pageF!==false || $pageD!==false){
			if($pageF!==false){
				$number = $this->pageNumber%2==0?$this->pageNumber/2:($this->pageNumber+1)/2;
				$DStart = $this->page - $number<0?$this->page - $number-1:0;
				if($this->page+$number-$DStart > $this->pageToatl){
					$DStop = ($this->page+$number-$DStart) - $this->pageToatl;
					$forStop = $this->pageToatl+1;
				}else{
					$DStop = 0;
					$forStop = $this->page+$number-$DStart+1;
				}
				$forStart = $this->page-$number-$DStop<1?1:$this->page-$number-$DStop;
				for($i=$forStart;$i<$forStop;++$i){
					if($i==$this->page){
						$numberF .= '<li class="active"><a href>'.$i.'</a></li>';
					}else{
						$numberF .= '<li><a href="'.$this->uri.'&'.$this->pageParam.'='.$i.'">'.$i.'</a></li>';
					}
				}
			}
			if($pageD!==false){
				$number = $this->pageNumber;
				$forStart = $this->page-$number>0?$this->page-$number:1;
				$forStop = $this->page+$number>$this->pageToatl?$this->pageToatl+1:$this->page+$number+1;
				for($i=$forStart;$i<$this->page;++$i){
					$numberD .= '<li><a href="'.$this->uri.'&'.$this->pageParam.'='.$i.'">'.$i.'</a></li>';
				}
				$numberD .= '<li class="active"><a href>'.$this->page.'</a></li>';
				$start = $this->page+1;
				for($i=$start;$i<$forStop;++$i){
					$numberD .= '<li><a href="'.$this->uri.'&'.$this->pageParam.'='.$i.'">'.$i.'</a></li>';
				}
			}
			$F = $forStart>1?'<li><a href="'.$this->uri.'&'.$this->pageParam.'=1">1</a></li>':'';
			$E = $forStop<$this->pageToatl+1?'<li><a href="'.$this->uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageToatl.'</a></li>':'';
			if($forStart>2){
				$omitF = '<span class="omit">…</span>';
				$startA = $this->page-$number<1?1:$this->page-$number;
				$omitFA = '<li><a class="omit" href="'.$this->uri.'&'.$this->pageParam.'='.$startA.'">…</a></li>';
			}
			if($forStop<$this->pageToatl){
				$omitE = '<span class="omit">…</span>';
				$stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
				$omitEA = '<li><a class="omit" href="'.$this->uri.'&'.$this->pageParam.'='.$stopA.'">…</a></li>';
			}
		}
		$this->pageType = str_ireplace(array('%F%','%E%','%omitFA%','%omitEA%','%omitF%','%omitE%','%numberF%','%numberD%'),array($F,$E,$omitFA,$omitEA,$omitF,$omitE,$numberF,$numberD),$this->pageType);
	}
 
	/**
	 *	处理url的方法
	 * @access public
	 * @param array   $url 	保持URL直对关系数组
	 * @return string 		过滤后的url尾参数
	 */
	private function uri(){
		$url = $_SERVER["REQUEST_URI"];
		$par = parse_url($url);
		if (isset($par['query'])) {
			parse_str($par['query'],$query);
			if(!is_array($this->uri)){
				$this->uri = array();
			}
			array_merge($query,$this->uri);
			unset($query[$this->pageParam]);
			while($key = array_search('',$query)){
				unset($query[$key]);
			}
			$this->uri = $par['path'].'?'.http_build_query($query);
		}else{
			$this->uri = $par['path'].'?';
		}
	}
 
	/**
	 * 设置limit方法及计算起始条数和结束条数
	 */
	private function calculate(){
		$this->pageToatl = ceil($this->total/$this->pageSize);
		$this->page = intval($_GET[$this->pageParam]);
		$this->page = $this->page>=1?$this->page>$this->pageToatl?$this->pageToatl:$this->page:1;
		$this->pageStart = ($this->page-1)*$this->pageSize;
		$this->pageStop = $this->pageStart+$this->pageSize;
		$this->pageStop = $this->pageStop>$this->total?$this->total:$this->pageStop;
		$this->limit = $this->pageStart.','.$this->pageStop;
	}
 
	/**
	 * 设置过滤器
	 */
	public function __set($name,$value){
		switch($name){
			case 'pageType':
			case 'uri':
				$this->$name = $value;
				return;
			case 'pageShow':
				if(is_array($value)){
					$this->pageShow = array_merge($this->pageShow,$value);
				}
				return;
		}
	}
 
	/**
	 * 取值过滤器
	 */
	public function __get($name){
		switch($name){
			case 'limit':
			case 'pageStart':
			case 'pageStop':
				return $this->$name;
			default:
				return;
		}
	}
}

发表评论

您的邮箱不会公开,当您的评论有新的回复时,会通过您填写的邮箱向您发送评论内容。 必填字段 *

为何看不到我发布的评论?

正在提交, 请稍候...