-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginate.php
174 lines (148 loc) · 6.56 KB
/
paginate.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
<?php
// *************************************************************
// file: paginate.php
// created by: Dan Palka
// used for: Gquip Senior Software project under the MIT license.
// date: 04-6-2014
// purpose: The paginate class serves as a method of paginating large amounts of SQL Data. The paginate class takes a count of the SQL data in the data to be returned and
// then displays this data in an organized fashion. It also returned an HTML number bar at the bottom of the page.
// *************************************************************
///////////////////////////////////////////////////////////////
// PHPagination v1.0 by Dan Palka, the most powerful wizard. //
// http://www.phpagination.com //
///////////////////////////////////////////////////////////////
function PHPagination($pCurrent, $pEnd, $PHPagi_urlPre, $PHPagi_urlPost, $PHPagi_uMultiplier = 1, $PHPagi_doArrows = TRUE) {
/////////////////////////////////////////////////
// LAYOUT SETTINGS - CHANGE TO SUIT YOUR NEEDS //
/////////////////////////////////////////////////
$PHPagi_htmlPre = '<div class="pagination-centered"><ul class="pagination">'; // Precedes pagination output. Default is '<div class="PHPagination">'.
$PHPagi_htmlPost = '</ul></div>'; // Suceeds pagination output. Default is '</div>'.
$PHPagi_htmlDivider = ' '; // Separates results. Default is '|'.
$PHPagi_htmlOmission = '<li class="unavailable"><a href="">…</a></li>'; // Replaces omitted results. Default is '$hellip;'.
$PHPagin_sPage1 = ' <strong>Page '; // Goes before current page. Default is ' <strong>Page '.
$PHPagin_sPage2 = ' of '; // Goes between current page and total pages. Default is ' of '.
$PHPagin_s_Page3 = '</strong> '; // Goes after total pages. Default is '</strong> '.
$PHPagi_leftArrow = '«'; // The left arrow. Default is '«'.
$PHPagi_rightArrow = '»'; // The right arrow. Default is '»'.
//////////////////////////////////////////
// DON'T CHANGE ANYTHING BELOW THIS BOX //
//////////////////////////////////////////
// Begin the PHPagination output.
$pOutput = $PHPagi_htmlPre;
// Check $pCurrent.
if(is_numeric($pCurrent) == FALSE || $pCurrent < -1) {
$pOutput = $pOutput . 'PHPagination Error: Current page' . $PHPagi_htmlPost;
return $pOutput;
}
// Check $pTotal.
if(is_numeric($pEnd) == FALSE || $pEnd < 0) {
$pOutput = $pOutput . 'PHPagination Error: End page' . $PHPagi_htmlPost;
return $pOutput;
}
// Check if current page is outside the range of pages.
if($pCurrent > $pEnd) {
// Return a nicely formatted error
$pOutput = $pOutput . 'PHPagination Error: Current page is greater than end page' . $PHPagi_htmlPost;
return $pOutput;
}
$pCurrent = floor($pCurrent / $PHPagi_uMultiplier);
$pEnd = floor($pEnd / $PHPagi_uMultiplier);
// Create "Page x of x" string since it's the same no matter what.
$pPageXofX = $PHPagin_sPage1 . number_format($pCurrent + 1) . $PHPagin_sPage2 . number_format($pEnd + 1) . $PHPagin_s_Page3;
// Build reusable chunks of hyperlink.
$pLink1 = ' <li><a href="' . $PHPagi_urlPre;
$pLink2 = $PHPagi_urlPost . '">';
$pLink3 = '</a></li> ';
// Do we need a left arrow?
if($PHPagi_doArrows == TRUE && $pCurrent > 0) {
$pOutput = $pOutput . $pLink1 . (($pCurrent - 1) * $PHPagi_uMultiplier) . $pLink2 . $PHPagi_leftArrow . $pLink3 . $PHPagi_htmlDivider;
}
// How many result units do we want?
if($pEnd >= 14) {
$pUnits = 14;
} else {
$pUnits = $pEnd;
}
// Which ellipses do we need?
if (($pEnd - $pUnits) >= 2) {
if ($pCurrent >= 8 && $pCurrent <= ($pEnd - 8)) {
$firstEllipsis = TRUE;
$secondEllipsis = TRUE;
} elseif ($pCurrent >= 8 && $pCurrent >= ($pEnd - 7)) {
$firstEllipsis = TRUE;
$secondEllipsis = FALSE;
} else {
$firstEllipsis = FALSE;
$secondEllipsis = TRUE;
}
} elseif (($pEnd - $pUnits) == 1) {
if ($pCurrent >= 8) {
$firstEllipsis = TRUE;
$secondEllipsis = FALSE;
} else {
$firstEllipsis = FALSE;
$secondEllipsis = TRUE;
}
} else {
$firstEllipsis = FALSE;
$secondEllipsis = FALSE;
}
// Where is PageXofX located?
if ($firstEllipsis == TRUE && $secondEllipsis == TRUE) {
$pageLocation = 7;
} elseif ($firstEllipsis == TRUE) {
$pageLocation = (14 - ($pEnd - $pCurrent));
} elseif ($firstEllipsis == FALSE && $pCurrent >= 0) {
$pageLocation = $pCurrent;
} else {
$pageLocation = -1;
}
// Loop through each result unit.
for($i = 0; $i <= $pUnits; $i++) {
if ($i == 3 && $firstEllipsis == TRUE) {
$pOutput = $pOutput . $PHPagi_htmlOmission;
$thisIsAnEllipsis = TRUE;
} elseif ($i == 11 && $secondEllipsis == TRUE) {
$pOutput = $pOutput . $PHPagi_htmlOmission;
$thisIsAnEllipsis = TRUE;
} else {
$thisIsAnEllipsis = FALSE;
}
if ($thisIsAnEllipsis == FALSE) {
if ($i > 0) {
if ($firstEllipsis == TRUE && $i == 4) {
} elseif ($secondEllipsis == TRUE && $i == 12) {
} else {
$pOutput = $pOutput . $PHPagi_htmlDivider;
}
}
if ($pageLocation == $i) {
$pOutput = $pOutput . $pPageXofX;
} elseif ($thisIsAnEllipsis == FALSE) {
if ($i <= 2) {
$pOutput = $pOutput . $pLink1 . ($i * $PHPagi_uMultiplier) . $pLink2 . number_format($i + 1) . $pLink3;
} elseif ($i >= 3 && $i <= 11) {
if ($firstEllipsis == TRUE && $secondEllipsis == TRUE) {
$pOutput = $pOutput . $pLink1 . (($pCurrent - (7 - $i)) * $PHPagi_uMultiplier) . $pLink2 . number_format(($pCurrent - (7 - $i)) + 1) . $pLink3;
} elseif ($firstEllipsis == TRUE && $secondEllipsis == FALSE) {
$pOutput = $pOutput . $pLink1 . (($pEnd - (14 - $i)) * $PHPagi_uMultiplier) . $pLink2 . number_format(($pEnd - (14 - $i)) + 1) . $pLink3;
} elseif ($firstEllipsis == FALSE && $secondEllipsis == TRUE) {
$pOutput = $pOutput . $pLink1 . ($i * $PHPagi_uMultiplier) . $pLink2 . number_format($i + 1) . $pLink3;
} else {
$pOutput = $pOutput . $pLink1 . ($i * $PHPagi_uMultiplier) . $pLink2 . number_format($i + 1) . $pLink3;
}
} elseif ($i >= 12) {
$pOutput = $pOutput . $pLink1 . (($pEnd - (14 - $i)) * $PHPagi_uMultiplier) . $pLink2 . number_format(($pEnd - (14 - $i)) + 1) . $pLink3;
}
}
}
}
// Do we need a right arrow?
if($PHPagi_doArrows == TRUE && $pCurrent < $pEnd) {
$pOutput = $pOutput . $PHPagi_htmlDivider . $pLink1 . (($pCurrent + 1) * $PHPagi_uMultiplier) . $pLink2 . $PHPagi_rightArrow . $pLink3;
}
// End the PHPagination output.
$pOutput = $pOutput . $PHPagi_htmlPost;
return $pOutput;
}
?>