Skip to content

Commit 2c3affb

Browse files
committed
Reduced pagination links
1 parent c9fb1f0 commit 2c3affb

File tree

3 files changed

+100
-61
lines changed

3 files changed

+100
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
./config
2+
.phpintel

customers.php

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
session_start();
33
require_once 'config/config.php';
4-
require_once BASE_PATH.'/includes/auth_validate.php';
4+
require_once BASE_PATH . '/includes/auth_validate.php';
55

66
// Costumers class
7-
require_once BASE_PATH.'/lib/Costumers/Costumers.php';
7+
require_once BASE_PATH . '/lib/Costumers/Costumers.php';
88
$costumers = new Costumers();
99

1010
// Get Input data from query string
@@ -13,41 +13,36 @@
1313
$order_by = filter_input(INPUT_GET, 'order_by');
1414

1515
// Per page limit for pagination.
16-
$pagelimit = 20;
16+
$pagelimit = 15;
1717

1818
// Get current page.
1919
$page = filter_input(INPUT_GET, 'page');
20-
if (!$page)
21-
{
22-
$page = 1;
20+
if (!$page) {
21+
$page = 1;
2322
}
2423

2524
// If filter types are not selected we show latest added data first
26-
if (!$filter_col)
27-
{
28-
$filter_col = 'id';
25+
if (!$filter_col) {
26+
$filter_col = 'id';
2927
}
30-
if (!$order_by)
31-
{
32-
$order_by = 'Desc';
28+
if (!$order_by) {
29+
$order_by = 'Desc';
3330
}
3431

3532
//Get DB instance. i.e instance of MYSQLiDB Library
3633
$db = getDbInstance();
37-
$select = array('id', 'f_name', 'l_name', 'gender', 'phone','created_at','updated_at');
34+
$select = array('id', 'f_name', 'l_name', 'gender', 'phone', 'created_at', 'updated_at');
3835

3936
//Start building query according to input parameters.
4037
// If search string
41-
if ($search_string)
42-
{
43-
$db->where('f_name', '%' . $search_string . '%', 'like');
44-
$db->orwhere('l_name', '%' . $search_string . '%', 'like');
38+
if ($search_string) {
39+
$db->where('f_name', '%' . $search_string . '%', 'like');
40+
$db->orwhere('l_name', '%' . $search_string . '%', 'like');
4541
}
4642

4743
//If order by option selected
48-
if ($order_by)
49-
{
50-
$db->orderBy($filter_col, $order_by);
44+
if ($order_by) {
45+
$db->orderBy($filter_col, $order_by);
5146
}
5247

5348
// Set pagination limit
@@ -57,7 +52,7 @@
5752
$rows = $db->arraybuilder()->paginate('customers', $page, $select);
5853
$total_pages = $db->totalPages;
5954

60-
include BASE_PATH.'/includes/header.php';
55+
include BASE_PATH . '/includes/header.php';
6156
?>
6257
<!-- Main container -->
6358
<div id="page-wrapper">
@@ -71,7 +66,7 @@
7166
</div>
7267
</div>
7368
</div>
74-
<?php include BASE_PATH.'/includes/flash_messages.php'; ?>
69+
<?php include BASE_PATH . '/includes/flash_messages.php';?>
7570

7671
<!-- Filters -->
7772
<div class="well text-center filter-form">
@@ -81,23 +76,23 @@
8176
<label for="input_order">Order By</label>
8277
<select name="filter_col" class="form-control">
8378
<?php
84-
foreach ($costumers->setOrderingValues() as $opt_value => $opt_name):
85-
($order_by === $opt_value) ? $selected = 'selected' : $selected = '';
86-
echo ' <option value="'.$opt_value.'" '.$selected.'>'.$opt_name.'</option>';
87-
endforeach;
88-
?>
79+
foreach ($costumers->setOrderingValues() as $opt_value => $opt_name):
80+
($order_by === $opt_value) ? $selected = 'selected' : $selected = '';
81+
echo ' <option value="' . $opt_value . '" ' . $selected . '>' . $opt_name . '</option>';
82+
endforeach;
83+
?>
8984
</select>
9085
<select name="order_by" class="form-control" id="input_order">
9186
<option value="Asc" <?php
92-
if ($order_by == 'Asc') {
93-
echo 'selected';
94-
}
95-
?> >Asc</option>
87+
if ($order_by == 'Asc') {
88+
echo 'selected';
89+
}
90+
?> >Asc</option>
9691
<option value="Desc" <?php
97-
if ($order_by == 'Desc') {
98-
echo 'selected';
99-
}
100-
?>>Desc</option>
92+
if ($order_by == 'Desc') {
93+
echo 'selected';
94+
}
95+
?>>Desc</option>
10196
</select>
10297
<input type="submit" value="Go" class="btn btn-primary">
10398
</form>
@@ -120,7 +115,7 @@
120115
<?php foreach ($rows as $row): ?>
121116
<tr>
122117
<td><?php echo $row['id']; ?></td>
123-
<td><?php echo htmlspecialchars($row['f_name'].' '.$row['l_name']); ?></td>
118+
<td><?php echo htmlspecialchars($row['f_name'] . ' ' . $row['l_name']); ?></td>
124119
<td><?php echo htmlspecialchars($row['gender']); ?></td>
125120
<td><?php echo htmlspecialchars($row['phone']); ?></td>
126121
<td>
@@ -151,34 +146,16 @@
151146
</div>
152147
</div>
153148
<!-- //Delete Confirmation Modal -->
154-
<?php endforeach; ?>
149+
<?php endforeach;?>
155150
</tbody>
156151
</table>
157152
<!-- //Table -->
158153

159154
<!-- Pagination -->
160155
<div class="text-center">
161-
<?php
162-
if (!empty($_GET)) {
163-
// We must unset $_GET[page] if previously built by http_build_query function
164-
unset($_GET['page']);
165-
// To keep the query sting parameters intact while navigating to next/prev page,
166-
$http_query = "?" . http_build_query($_GET);
167-
} else {
168-
$http_query = "?";
169-
}
170-
// Show pagination links
171-
if ($total_pages > 1) {
172-
echo '<ul class="pagination text-center">';
173-
for ($i = 1; $i <= $total_pages; $i++) {
174-
($page == $i) ? $li_class = ' class="active"' : $li_class = '';
175-
echo '<li' . $li_class . '><a href="customers.php' . $http_query . '&page=' . $i . '">' . $i . '</a></li>';
176-
}
177-
echo '</ul>';
178-
}
179-
?>
156+
<?php echo paginationLinks($page, $total_pages, 'customers.php'); ?>
180157
</div>
181158
<!-- //Pagination -->
182159
</div>
183160
<!-- //Main container -->
184-
<?php include BASE_PATH.'/includes/footer.php'; ?>
161+
<?php include BASE_PATH . '/includes/footer.php';?>

helpers/helpers.php

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ function randomString($n) {
2323
return $generated_string;
2424
}
2525

26-
2726
/**
28-
*
27+
*
2928
*/
3029
function getSecureRandomToken() {
3130
$token = bin2hex(openssl_random_pseudo_bytes(16));
3231
return $token;
3332
}
3433

3534
/**
36-
* Clear Auth Cookie
35+
* Clear Auth Cookie
3736
*/
3837
function clearAuthCookie() {
3938

@@ -43,11 +42,73 @@ function clearAuthCookie() {
4342
setcookie('remember_token', null, -1, '/');
4443
}
4544
/**
46-
*
45+
*
4746
*/
4847
function clean_input($data) {
4948
$data = trim($data);
5049
$data = stripslashes($data);
5150
$data = htmlspecialchars($data);
5251
return $data;
52+
}
53+
54+
function paginationLinks($current_page, $total_pages, $base_url) {
55+
56+
$html = '';
57+
58+
if ($total_pages <= 1) {
59+
return '';
60+
}
61+
62+
$html = '<ul class="pagination text-center">';
63+
64+
if ($current_page == 1) {
65+
66+
$html .= '<li class="disabled"><a>First</a></li>';
67+
} else {
68+
$html .= '<li><a href="' . '?page=1">First</a></li>';
69+
}
70+
71+
if (!empty($_GET)) {
72+
// We must unset $_GET[page] if previously built by http_build_query function
73+
unset($_GET['page']);
74+
// To keep the query sting parameters intact while navigating to next/prev page,
75+
$http_query = "?" . http_build_query($_GET);
76+
} else {
77+
$http_query = "?";
78+
}
79+
// Show pagination links
80+
81+
//var i = (Number(data.page) > 5 ? Number(data.page) - 4 : 1);
82+
83+
if ($current_page > 5) {
84+
$i = $current_page - 4;
85+
} else {
86+
$i = 1;
87+
}
88+
89+
for (; $i <= ($current_page + 4) && ($i <= $total_pages); $i++) {
90+
($current_page == $i) ? $li_class = ' class="active"' : $li_class = '';
91+
92+
$link = $base_url . $http_query;
93+
94+
$html = $html . '<li' . $li_class . '><a href="' . $link . '&page=' . $i . '">' . $i . '</a></li>';
95+
96+
if ($i == $current_page + 4 && $i < $total_pages) {
97+
98+
$html = $html . '<li class="disabled"><a>...</a></li>';
99+
100+
}
101+
102+
}
103+
104+
if ($current_page == $total_pages) {
105+
$html .= '<li class="disabled"><a>Last</a></li>';
106+
} else {
107+
108+
$html .= '<li><a href="' . $base_url . $http_query . '&page=' . $total_pages . '">Last</a></li>';
109+
}
110+
111+
$html = $html . '</ul>';
112+
113+
return $html;
53114
}

0 commit comments

Comments
 (0)