This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
PageBar.php
236 lines (202 loc) · 7.7 KB
/
PageBar.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
function getPageBar($mysqli = '', $sql = "", $show_num = 20, $page_list = 10, $to_page = "", $url_other = "")
{
//die('PHP_SELF:'.$_SERVER['PHP_SELF']);
if (empty($show_num)) {
$show_num = 20;
}
if (empty($page_list)) {
$page_list = 10;
}
$result = $mysqli->query($sql) or die($mysqli->connect_error);
$total = $result->num_rows;
$navbar = new PageBar($total, $show_num, $page_list);
if (!empty($to_page)) {
$navbar->set_to_page($to_page);
}
if (!empty($url_other)) {
$navbar->set_url_other($url_other);
}
$mybar = $navbar->makeBar();
$main['bar'] = "
<div class='row'>
<div class='container'>
<div class='empty-space col-xs-b55 col-sm-b110'></div>
<div class='page-pagination' id='pagination'>
{$mybar['left']}
{$mybar['center']}
{$mybar['right']}
</div>
<div class='empty-space col-xs-b55 col-sm-b110'></div>
</div>
</div>
";
$main['sql'] = $sql . $mybar['sql'];
$main['total'] = $total;
return $main;
}
class PageBar
{
// 目前所在頁碼
public $current;
// 所有的資料數量 (rows)
public $total;
// 每頁顯示幾筆資料
public $limit = 10;
// 目前在第幾層的頁數選項?
public $pCurrent;
// 總共分成幾頁?
public $pTotal;
// 每一層最多有幾個頁數選項可供選擇,如:3 = {[1][2][3]}
public $pLimit;
// 要使用的 URL 頁數參數名?
public $url_page = "g2p";
// 會使用到的 URL 變數名,給 process_query() 過濾用的。
public $used_query = array();
public $query_str; // 存放 URL 參數列
//指定頁面
public $to_page;
//其他連結參數
public $url_other;
public function __construct($total, $limit = 10, $page_limit)
{
$limit = intval($limit);
//die(var_export($limit));
$mydirname = basename(dirname(__FILE__));
$this->to_page = $_SERVER['PHP_SELF'];
$this->limit = $limit;
$this->total = $total;
$this->pLimit = $page_limit;
}
public function init()
{
$this->used_query = array($this->url_page);
$this->query_str = $this->processQuery($this->used_query);
$this->glue = ($this->query_str == "") ? '?' : '&';
$this->current = (isset($_GET[$this->url_page])) ? intval($_GET[$this->url_page]) : 1;
if ($this->current < 1) {
$this->current = 1;
}
$this->pTotal = ceil($this->total / $this->limit);
$this->pCurrent = ceil($this->current / $this->pLimit);
}
// 處理 URL 的參數,過濾會使用到的變數名稱
public function processQuery($used_query)
{
// 將 URL 字串分離成二維陣列
$QUERY_STRING = htmlspecialchars($_SERVER['QUERY_STRING']);
$vars = explode("&", $QUERY_STRING);
//die(var_export($vars));
for ($i = 0; $i < count($vars); $i++) {
if (substr($vars[$i], 0, 7) == "amp;g2p") {
continue;
}
//echo substr($vars[$i],0,7)."<br>";
$var[$i] = explode("=", $vars[$i]);
}
// 過濾要使用的 URL 變數名稱
for ($i = 0; $i < count($var); $i++) {
for ($j = 0; $j < count($used_query); $j++) {
if (isset($var[$i][0]) && $var[$i][0] == $used_query[$j]) {
$var[$i] = array();
}
}
}
$vars = array();
// 合併變數名與變數值
for ($i = 0; $i < count($var); $i++) {
$vars[$i] = implode("=", $var[$i]);
}
// 合併為一完整的 URL 字串
$processed_query = "";
for ($i = 0; $i < count($vars); $i++) {
$glue = ($processed_query == "") ? '?' : '&';
// 開頭第一個是 '?' 其餘的才是 '&'
if ($vars[$i] != "") {
$processed_query .= $glue . $vars[$i];
}
}
return $processed_query;
}
// 製作 sql 的 query 字串 (LIMIT)
public function sqlQuery()
{
$row_start = ($this->current * $this->limit) - $this->limit;
$sql_query = " LIMIT {$row_start}, {$this->limit}";
return $sql_query;
}
public function set_to_page($page = "")
{
$this->to_page = $page;
}
public function set_url_other($other = "")
{
$this->url_other = $other;
}
// 製作 bar
public function makeBar($url_page = "none")
{
if ($url_page != "none") {
$this->url_page = $url_page;
}
$this->init();
// 取得其他連結參數
$loadtime = $this->url_other;
// 取得目前頁框(層)的第一個頁數啟始值,如 6 7 8 9 10 = 6
$i = ($this->pCurrent * $this->pLimit) - ($this->pLimit - 1);
$bar_center = "";
while ($i <= $this->pTotal && $i <= ($this->pCurrent * $this->pLimit)) {
if ($i == $this->current) {
$bar_center = "
{$bar_center}
<a href='{$this->to_page}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='{$i}' class='active'>{$i}<span class='sr-only'>(current)</span></a>";
} else {
$bar_center .= "
<a href='{$this->to_page}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='{$i}' class='page-link'>{$i}</a>";
}
$i++;
}
$bar_center = $bar_center . "";
// 往前跳一頁
if ($this->current <= 1) {
//$bar_left=$bar_first="";
$bar_left = "<!--<a href='javascript:;' class='page-link disabled'>‹上一頁</a>-->";
$bar_first = "<!--<a href='javascript:;' class='page-link disabled'>«第一頁</a>-->";
} else {
$i = $this->current - 1;
$bar_left = "<a href='{$this->to_page}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='回上頁' class='page-link'>上一頁</a>";
$bar_first = "<a href='{$this->to_page}{$this->query_str}{$this->glue}{$this->url_page}=1{$loadtime}' title='回第一頁' class='page-link'>第一頁</a>";
}
// 往後跳一頁
if ($this->current >= $this->pTotal) {
//$bar_right=$bar_last="";
$bar_right = "<!--<a href='javascript:;' class='page-link disabled'>下一頁</a>-->";
$bar_last = "<!--<a href='javascript:;' class='page-link disabled'>最末頁</a>-->";
} else {
$i = $this->current + 1;
$bar_right = "<a href='{$this->to_page}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='下一頁' class='page-link'>下一頁</a>";
$bar_last = "<a href='{$this->to_page}{$this->query_str}{$this->glue}{$this->url_page}={$this->pTotal}{$loadtime}' title='最末頁' class='page-link'>最末頁</a>";
}
// 往前跳一整個頁框(層)
if (($this->current - $this->pLimit) < 1) {
$bar_l = "";
} else {
$i = $this->current - $this->pLimit;
$bar_l = "";
}
//往後跳一整個頁框(層)
if (($this->current + $this->pLimit) > $this->pTotal) {
$bar_r = "";
} else {
$i = $this->current + $this->pLimit;
$bar_r = "";
}
$page_bar['center'] = $bar_center;
$page_bar['left'] = $bar_first . $bar_l . $bar_left;
$page_bar['right'] = $bar_right . $bar_r . $bar_last;
$page_bar['current'] = $this->current;
$page_bar['total'] = $this->pTotal;
$page_bar['sql'] = $this->sqlQuery();
return $page_bar;
}
}