-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagination.php
66 lines (52 loc) · 1.49 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
<?php
/**
* Pagination
* @author Gökhan Kaya <gkxdev@gmail.com>
*/
class Pagination {
const QUERY_STRING = 'page';
private $total, $limit, $url;
private $pages, $page;
public function __construct($total, $limit = 10, $url = '') {
$this->total = $total;
$this->limit = $limit;
$this->url = $url;
$this->pages = ceil($total / $limit);
$this->page = min(
max((int) @$_GET[self::QUERY_STRING], 1),
$this->pages
);
}
public function limit() {
return $this->limit;
}
public function offset() {
return ($this->page - 1) * $this->limit;
}
public function html() {
$prev = $next = null;
if ($this->page > 1) {
$prev = $this->makeLink($this->page - 1, '← Geri');
}
if ($this->page < $this->pages) {
$next = $this->makeLink($this->page + 1, 'İleri →');
}
$info = sprintf('<span class="pagination-info">%d / %d</span>',
$this->page,
$this->pages
);
return sprintf('<div class="pagination">%s%s%s</div>',
$prev, $info, $next
);
}
private function makeLink($number, $text) {
$qs = (bool) strpos($this->url, '?')
? '&'
: '?';
return sprintf('<a class="pagination-item" href="%s=%d">%s</a>',
$this->url . $qs . self::QUERY_STRING,
$number,
$text
);
}
}