Paging.php
2.92 KB
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
<?php
/**
* 分页基类
*
* example:
* <pre>
* use Hood\Paging;
* $paging = new Paging();
* $paging->setTotal(10)->setCursor(1);
* </pre>
*
* @name Paging
* @version 2.0 (2009-5-11 下午05:47:31)
* @author ZIP
* @since 1.0
*/
namespace Hood;
class Paging {
/**
* 总分页数
*
* @var Integer
*/
private $total;
/**
* 每页记录数
*
* @var Integer
*/
private $size;
/**
* 当前页
* @var Integer
*/
protected $currentPage = 1;
/**
* 光标
*
* @var Integer
*/
private $cursor = 0;
/**
* 当前页
* @return Integer
*/
public function getCurrent() {
return $this->currentPage;
}
/**
* 设置当前页
* @param Integer $pageNo
* @return Paging
*/
public function setCurrent( $pageNo ) {
$cur = (int) intval($pageNo);
if ($cur <= 0) {
$cur = 1;
}
$this->currentPage = $cur;
return $this;
}
/**
* 下一页
* @return Integer
*/
public function getNext() {
$pageNum = $this->getPageNum();
$current = $this->getCurrent();
return $current < $pageNum ? ($current + 1) : $pageNum;
}
/**
* 上一页
* @return Integer
*/
public function getPrev() {
$current = $this->getCurrent();
return $current > 1 ? ($current - 1) : 1;
}
/**
* 取得记录开始的偏移量
* @return Integer
*/
public function getOffset() {
$offset = $this->getSize() * abs($this->getCurrent() - 1);
if ($offset >= $this->getTotal()) {
$offset = 0;
if ($this->getTotal() > 0 && $this->getTotal() > $this->getSize()) {
$offset = $this->getSize() * abs($this->getPageNum() - 1);
}
}
return (int) abs($offset);
}
/**
* 设置总记录数
*
* @param Integer $total
* @return Paging
*/
public function setTotal( $total ) {
$this->total = (int) intval($total);
return $this;
}
/**
* 获取总数
* @return Integer
*/
public function getTotal() {
return (int) intval($this->total);
}
/**
* 获取每页显示数
* @return Integer
*/
public function getSize() {
return (int) intval($this->size);
}
/**
* 设置每页记录数
*
* @param Integer $size
* @return Paging
*/
public function setSize( $size ) {
if ($size > 0) {
$this->size = (int) intval($size);
}
return $this;
}
/**
* 获取起始数
* @return Integer
**/
public function getStarting() {
return $this->getOffset();
}
/**
* 获取终点数
* @return Integer
**/
public function getEnding() {
return $this->getOffset() + $this->getSize();
}
/**
* 光标
*
* @return Integer
*/
public function getCursor() {
return $this->cursor;
}
/**
* 设置光标
*
* @param Integer $cursor
* @return Paging
*/
public function setCursor( $cursor ) {
$this->cursor = intval($cursor);
return $this;
}
/**
* 取得总分页数
*
* @return Integer
*/
public function getPageNum() {
if ($this->getSize() == 0) {
return 0;
}
return ceil($this->getTotal() / $this->getSize());
}
}