-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.class.php
85 lines (78 loc) · 3.16 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
class pagination {
private $num_pages = 1;
private $start = 0;
private $display;
private $start_display;
function __construct ($query, $display=10) {
if (!empty($query)) {
$this->display = $display;
if (isset($_GET['display']) && is_numeric($_GET['display'])) $this->display = (int) $_GET['display'];
if (isset($_GET['np']) && is_numeric($_GET['np']) && $_GET['np'] > 0) {
$this->num_pages = (int) $_GET['np'];
} else {
if (is_numeric($query)) {
$num_records = $query;
} else {
$result = db_query ($query);
if ($result->num_rows > 1 || strstr($query, 'COUNT') === false) {
$num_records = $result->num_rows;
} else {
$row = $result->fetch_row();
$num_records = $row[0];
}
}
if ($num_records > $this->display) $this->num_pages = ceil ($num_records/$this->display);
}
if (isset($_GET['s']) && is_numeric($_GET['s']) && $_GET['s'] > 0) $this->start = (int) $_GET['s'];
$this->start_display = " LIMIT {$this->start}, {$this->display}";
}
}
public function display ($split=5) {
global $page;
$html = '';
if ($this->num_pages <= 1) return $html;
// $page->link('pagination.css');
$url = $page->url ('add', '', 'np', $this->num_pages);
$current_page = ($this->start/$this->display) + 1;
$begin = $current_page - $split;
$end = $current_page + $split;
if ($begin < 1) {
$begin = 1;
$end = $split * 2;
}
if ($end > $this->num_pages) {
$end = $this->num_pages;
$begin = $end - ($split * 2);
$begin++; // add one so that we get double the split at the end
if ($begin < 1) $begin = 1;
}
if ($current_page != 1) {
$html .= '<a class="first" title="First" href="' . $page->url('add', $url, 's', 0) . '">«</a>';
$html .= '<a class="prev" title="Previous" href="' . $page->url('add', $url, 's', $this->start - $this->display) . '">Previous</a>';
} else {
$html .= '<span class="disabled first" title="First">«</span>';
$html .= '<span class="disabled prev" title="Previous">Previous</span>';
}
for ($i=$begin; $i<=$end; $i++) {
if ($i != $current_page) {
$html .= '<a title="' . $i . '" href="' . $page->url('add', $url, 's', ($this->display * ($i - 1))) . '">' . $i . '</a>';
} else {
$html .= '<span class="current">' . $i . '</span>';
}
}
if ($current_page != $this->num_pages) {
$html .= '<a class="next" title="Next" href="' . $page->url('add', $url, 's', $this->start + $this->display) . '">Next</a>';
$last = ($this->num_pages * $this->display) - $this->display;
$html .= '<a class="last" title="Last" href="' . $page->url('add', $url, 's', $last) . '">»</a>';
} else {
$html .= '<span class="disabled next" title="Next">Next</span>';
$html .= '<span class="disabled last" title="Last">»</span>';
}
return '<div class="pagination">' . $html . '</div>';
}
public function limit () {
return $this->start_display;
}
}
?>