-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
94 lines (84 loc) · 2.79 KB
/
functions.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
<?
function sanitize($data) {
return htmlspecialchars(stripslashes(trim($data)));
}
function isPositiveValue($value){
return ((int)$value == $value && (int)$value > 0);
}
function paginate($connection,$page,$type,$word = null){
$limit = 10;
$adjacents = 1;
if($type=="all")
$total_rows = $connection->numRows("SELECT id FROM clients");
else if ($type=="search")
$total_rows = $connection->numRows("SELECT id FROM clients WHERE lastname LIKE '%".$word."%' ");
$total_pages = ceil($total_rows / $limit);
if(isset($page) && $page != "") {
$offset = $limit * ($page-1);
} else {
$page = 1;
$offset = 0;
}
if($total_pages <= (1+($adjacents * 2))) {
$start = 1;
$end = $total_pages;
} else {
if(($page - $adjacents) > 1) {
if(($page + $adjacents) < $total_pages) {
$start = ($page - $adjacents);
$end = ($page + $adjacents);
} else {
$start = ($total_pages - (1+($adjacents*2)))+1;
$end = $total_pages;
}
} else {
$start = 1;
$end = (1+($adjacents * 2));
}
}
if($total_pages > 1) {
echo "<nav aria-label='Page navigation'>";
echo "<ul class='pagination justify-content-center'>\n";
echo "<!-- Link of the first page -->\n";
echo "<li class='page-item ";
($page <= 1 ? print 'disabled' : '');
echo "'>\n";
echo "<a class='page-link' href='clients.php?page=1'><<</a>\n";
echo "</li>\n";
echo "<!-- Link of the previous page -->\n";
echo "<li class='page-item ";
($page <= 1 ? print 'disabled' : '');
echo "'>\n";
echo "<a class='page-link' href='clients.php?page=";
($page>1 ? print($page-1) : print 1);
echo "'><</a>\n";
echo "</li>\n";
echo "<!-- Links of the pages with page number -->";
for($i=$start; $i<=$end; $i++) {
echo "<li class='page-item ";
($i == $page ? print 'active' : '');
echo "'>";
echo "<a class='page-link' href='clients.php?page=$i'>$i</a>";
echo "</li>";
}
echo "<!-- Link of the next page -->\n";
echo "<li class='page-item ";
($page >= $total_pages ? print 'disabled' : '');
echo"'>\n";
echo "<a class='page-link' href='clients.php?page=";
($page < $total_pages ? print($page+1) : print $total_pages);
echo "'>></a>\n";
echo "</li>\n";
echo "<!-- Link of the last page -->\n";
echo "<li class='page-item ";
($page >= $total_pages ? print 'disabled' : '');
echo "'>\n";
echo "<a class='page-link' href='clients.php?page=$total_pages'>>>\n";
echo "</a>\n";
echo "</li>\n";
echo "</ul>";
echo "</nav>";
}
return $offset;
}
?>