-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination.class.php
62 lines (56 loc) · 1.87 KB
/
pagination.class.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
<?php
class Pagination
{
public $count = 6; // Number Of Posts Per Page
public $url = 'www.domain.com?action=news&page='; // Address Of All Posts With $_GET['page']
public $total = 0; // Number Of Total Posts (SELECT COUNT(*) AS count FROM posts)
// define current page number
private function define_page()
{
isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page']) ? $page = (int)$_GET['page'] : $page = 1;
return $page;
}
// get max page number
private function limit_page()
{
$page = $this->define_page();
$start = $page * $this->count - $this->count;
$fullnumber = $this->total;
$total = $fullnumber['count'] / $this->count;
is_float($total) ? $max = $total + 2 : $max = $total + 1;
$array = array('start'=>(int)$start,'max'=>(int)$max);
return $array;
}
// view html code
public function view()
{
$page = $this->define_page();
$limit = $this->limit_page();
if($limit['max'] < 6)
{
$max=$limit['max'];
$min=0;
}else
{
$page+4 > $limit['max'] ? $max=$limit['max'] : $max=$page+4;
$page-4 > 0 ? $min=$page-4 : $min=0;
}
print '<ul class="pagination">';
$next=$page+1;
$prev=$page-1;
if($page!=1){echo'<li><a href="'.$this->url.$prev.'">Prev</a></li>';}
for($i=$min+1;$i<$max;$i++)
{
if($page == $i)
{
echo '<li class="active"><a href="#">'.$i.'</a></li>';
}else
{
echo '<li><a href="'.$this->url.$i.'">'.$i.'</a></li>';
}
}
$last = $limit['max']-1;
if($page!=$last && $page<$limit['max']){print '<li><a href="'.$this->url.$next.'">Next</a></li>';}
print '</ul>';
}
}