This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpagination.php
123 lines (99 loc) · 2.26 KB
/
pagination.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
<?php
class Pagination {
public static function factory($total, $limit = 5, $page = 1, $adjacents = 2)
{
return new Pagination($total, $limit, $page, $adjacents);
}
public $total;
public $limit;
public $page;
public $adjacents;
public function __construct($total, $limit, $page, $adjacents)
{
$this->total = (int) $total;
$this->limit = (int) $limit;
$this->page = (int) $page;
$this->adjacents = (int) $adjacents;
if ($this->page < 1)
{
$this->page = 1;
}
}
public function render()
{
$last = ceil($this->total / $this->limit);
if ($last < 2 or $this->page > $last)
{
return FALSE;
}
$arr = array(
'current' => $this->page,
'limit' => $this->limit,
'total' => $this->total,
'pages' => $last
);
if ($this->page > 1)
{
$arr['previous'] = $this->page - 1;
}
if ($this->page < $last)
{
$arr['next'] = $this->page + 1;
}
$r1 = ($this->page - 1) * $this->limit + 1;
$r2 = $r1 + $this->limit - 1;
$r2 = ($this->total < $r2) ? $this->total : $r2;
$arr['displaying'] = array(
'items' => $r2 - $r1 + 1,
'from' => $r1,
'to' => $r2
);
if ($last < 5 + ($this->adjacents * 2))
{
for ($counter = 1; $counter <= $last; $counter++)
{
$arr['links'][] = $this->_numbers($counter);
}
}
elseif ($last >= 5 + ($this->adjacents * 2))
{
if ($this->page < 1 + ($this->adjacents * 2))
{
$arr['last'] = TRUE;
for ($counter = 1; $counter < 2 + ($this->adjacents * 2); $counter++)
{
$arr['links'][] = $this->_numbers($counter);
}
}
elseif ($last - ($this->adjacents * 2) >= $this->page AND $this->page > ($this->adjacents * 2))
{
$arr['first'] = TRUE;
$arr['last'] = TRUE;
for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
{
$arr['links'][] = $this->_numbers($counter);
}
}
else
{
$arr['first'] = TRUE;
for ($counter = $last - ($this->adjacents * 2); $counter <= $last; $counter++)
{
$arr['links'][] = $this->_numbers($counter);
}
}
}
return $arr;
}
protected function _numbers($num)
{
if ($num == $this->page)
{
return array('active' => TRUE, 'number' => $num);
}
else
{
return array('number' => $num);
}
}
} // End Pagination