#
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
| // PHP CODE Highliting for CU by dZ902
<?
/**
*
*
* @version $Id$
* @copyright 2006
*/
/**
* What we need to input?
* - which page is currently in?
* - how many records are there?
* - how many records perpage?
* - how many page numbers in a list?
* What we need to output?
* - a list of links to different pages
* - previous/next page links (optional)
* - first/last page links (optional)
**/
class Pager
{
var $numItems = 1000;
var $itemsPerPage = 10;
var $numPages;
var $currPageId = 1;
var $prevPageId = 0;
var $nextPageId = 0;
var $pagesPerList = 3;
var $list = array(); // example
function Pager($params)
{
$this->numItems = $params['numItems'];
$this->itemsPerPage = $params['itemsPerPage'];
$this->pagesPerList = $params['pagesPerList'];
$this->currPageId = $params['currPageId'];
$this->numPages = ceil($this->numItems / $this->itemsPerPage);
}
function getList()
{
if ($this->currPageId > $this->numPages || $this->currPageId < 0) {
// error: invalid page input [current page]
}
if ($this->numPages == 1) {
// only one page
$this->prevPageId = -1;
$this->nextPageId = -1;
} elseif ($this->currPageId == 1) {
// at first page
$this->prevPageId = -1;
$this->nextPageId = $this->currPageId + 1;
} elseif ($this->currPageId == $this->numPages) {
// at last page
$this->prevPageId = $this->currPageId - 1;
$this->nextPageId = -1;
} else {
// normal
$this->prevPageId = $this->currPageId - 1;
$this->nextPageId = $this->currPageId + 1;
}
if ($this->numPages < $this->pagesPerList) {
// total pages is within capacity - show all pages
$this->list = range(1, $this->numPages);
} else {
if ($this->pagesPerList % 2 != 0) {
// even bounds, current page is at center
$bound = floor($this->pagesPerList / 2);
$low = $this->currPageId - $bound;
$high = $this->currPageId + $bound;
} else {
// odd bounds, split it as -1 and +1
$bound = ($this->pagesPerList / 2) - 1;
$low = $this->currPageId - $bound;
$high = $this->currPageId + $bound + 1;
}
if ($low < 1) {
// lower bound overrun
$high = $high + (-$low) + 1;
$low = 1;
} elseif ($high > $this->numPages) {
// higher bound overrun
$low = $low - ($high - $this->numPages);
$high = $this->numPages;
}
if ($this->currPageId == 0) {
// plain list
++$high;
}
$this->list = range($low, $high);
}
return $this->list;
}
function isFirstPage()
{
if (empty($this->list)) {
// error [pager data not loaded]
} else {
return ($this->prevPageId == -1);
}
}
function isLastPage()
{
if (empty($this->list)) {
// error [pager data not loaded]
} else {
return ($this->nextPageId == -1);
}
}
function _debug()
{
$tmp = &new Pager(array('numItems' => 150,
'itemsPerPage' => 10,
'pagesPerList' => 5,
'currPageId' => 0));
for ($a = 'a', $b = 1; $a < 'h', $b < 15; ++$a, ++$b) {
$param = array('numItems' => 150,
'itemsPerPage' => 10,
'pagesPerList' => 5,
'currPageId' => $b);
$tmp->Pager($param);
$tmp->getList();
echo "{$a}) ";
if (!$tmp->isFirstPage()) {
echo "<< ";
} else {
echo " ";
}
foreach($tmp->list as $l){
if ($tmp->currPageId == $l) {
echo " {{$l}} ";
} else {
echo " {$l} ";
}
}
echo "\n";
}
}
}
Pager::_debug();
?>
|