-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pagination.php
129 lines (115 loc) · 3.85 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
124
125
126
127
128
129
<?php
/**
* Pagination basic created by toidicode.com
*
*
* @package Pagination basic
* @author ThanhTai - ToiDiCodeTeam
* @copyright Copyright (c) 2017, Toidicode Team. (http://toidicode.com/)
* @link https://toidicode.com
* @since Version 1.0.0
* @filesource
*/
class Pagination
{
/**
* Biến config chứa tất cả các cấu hình
* @var array
*/
private $config = [
'total' => 0,
'limit' => 0,
'full' => false,
'querystring' => 'page',
];
/**
* khởi tạo
* @param array $config
*/
public function __construct($config = [])
{
if (isset($config['limit']) && $config['limit'] < 0 || isset($config['total']) && $config['total'] < 0) {
die('limit và total không được nhỏ hơn 0');
}
if (!isset($config['querystring'])) {
$config['querystring'] = 'page';
}
$this->config = $config;
}
/**
* Lấy ra tổng số trang
* @return int
*/
private function gettotalPage()
{
return ceil($this->config['total'] / $this->config['limit']);
}
/**
* Lấy ra trang hiện tại
* @return int
*/
private function getCurrentPage()
{
if (isset($_GET[$this->config['querystring']]) && (int) $_GET[$this->config['querystring']] >= 1) {
if ((int) $_GET[$this->config['querystring']] > $this->gettotalPage()) {
return (int) $this->gettotalPage();
} else {
return (int) $_GET[$this->config['querystring']];
}
} else {
return 1;
}
}
/**
* lấy ra trang phía trước
* @return string
*/
private function getPrePage()
{
if ($this->getCurrentPage() === 1) {
return;
} else {
return '<li><a href="' . $_SERVER['PHP_SELF'] . '?' . $this->config['querystring'] . '=' . ($this->getCurrentPage() - 1) . '" >Pre</a></li>';
}
}
/**
* Lấy ra trang phía sau
* @return string
*/
private function getNextPage()
{
if ($this->getCurrentPage() >= $this->gettotalPage()) {
return;
} else {
return '<li><a href="' . $_SERVER['PHP_SELF'] . '?' . $this->config['querystring'] . '=' . ($this->getCurrentPage() + 1) . '" >Next</a></li>';
}
}
/**
* Hiển thị html code của page
* @return string
*/
public function getPagination()
{
$data = '';
if (isset($this->config['full']) && $this->config['full'] === false) {
$data .= ($this->getCurrentPage() - 3) > 1 ? '<li>...</li>' : '';
for ($i = ($this->getCurrentPage() - 3) > 0 ? ($this->getCurrentPage() - 3) : 1; $i <= (($this->getCurrentPage() + 3) > $this->gettotalPage() ? $this->gettotalPage() : ($this->getCurrentPage() + 3)); $i++) {
if ($i === $this->getCurrentPage()) {
$data .= '<li class="active" ><a href="#" >' . $i . '</a></li>';
} else {
$data .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?' . $this->config['querystring'] . '=' . $i . '" >' . $i . '</a></li>';
}
}
$data .= ($this->getCurrentPage() + 3) < $this->gettotalPage() ? '<li>...</li>' : '';
} else {
for ($i = 1; $i <= $this->gettotalPage(); $i++) {
if ($i === $this->getCurrentPage()) {
$data .= '<li class="active" ><a href="#" >' . $i . '</a></li>';
} else {
$data .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?' . $this->config['querystring'] . '=' . $i . '" >' . $i . '</a></li>';
}
}
}
return '<ul>' . $this->getPrePage() . $data . $this->getNextPage() . '</ul>';
}
}