forked from aichallenge/aichallenge
/
pagination.php
89 lines (84 loc) · 4.01 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
<?php
function getPaginationString($page = 1, $lastpage, $perpage = 50, $pagestring = "?page=", $targetpage=NULL)
{
//other vars
if ($targetpage === NULL) {
$targetpage = $_SERVER['PHP_SELF'];
}
$adjacents = 2;
$prev = $page - 1;
$next = $page + 1;
$lpm1 = $lastpage - 1;
$pagestring = ($pagestring == null)?"?page=":$pagestring;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination .= "<span><a href=\"$targetpage$pagestring$prev\">« prev</a></span>";
else
$pagination .= "<span class=\"disabled\">« prev</span>";
if ($lastpage < 5 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></span>";
}
}
elseif($lastpage >= 5 + ($adjacents * 2)) //enough pages to hide some
{
if($page < 1 + ($adjacents * 3))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></span>";
}
$pagination .= "<span class=\"elipses\">...</span>";
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a></span>";
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a></span>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . "1\">1</a></span>";
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . "2\">2</a></span>";
$pagination .= "<span class=\"elipses\">...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></span>";
}
$pagination .= "...";
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a></span>";
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a></span>";
}
else
{
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . "1\">1</a></span>";
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . "2\">2</a></span>";
$pagination .= "<span class=\"elipses\">...</span>";
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></span>";
}
}
}
if ($page < $counter - 1)
$pagination .= "<span><a href=\"" . $targetpage . $pagestring . $next . "\">next »</a></span>";
else
$pagination .= "<span class=\"disabled\">next »</span>";
$pagination .= "</div>\n";
}
return $pagination;
}
?>