-
Notifications
You must be signed in to change notification settings - Fork 6
/
pagination.php
180 lines (164 loc) · 6.58 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
require_once "logic.php";
class bootPagination
{
public $pagenumber;
public $pagesize;
public $totalrecords;
public $showfirst;
public $showlast;
public $paginationcss;
public $paginationstyle;
public $defaultUrl;
public $paginationUrl;
// pager style
public $prevCss;
public $nextCss;
function __construct()
{
$this->pagenumber = 1;
$this->pagesize = 20;
$this->totalrecords = 0;
$this->showfirst = true;
$this->showlast = true;
$this->paginationcss = "pagination-small";
$this->paginationstyle = 0; // 1: advance, 0: normal, 2: pager
$this->defaultUrl = "#"; // in case of ajax pagination
$this->paginationUrl = "#"; // # incase of ajax pagination e.g index.php?p=[p] -->
$this->prevCss = "previous";
$this->nextCss = "next";
}
function process()
{
$paginationlst = "";
$firstbound =0;
$lastbound =0;
$tooltip = "";
if($this->totalrecords > $this->pagesize)
{
$totalpages = ceil($this->totalrecords / $this->pagesize);
if ($this->pagenumber > 1)
{
if ($this->showfirst && $this->paginationstyle != 2)
{
$firstbound = 1;
$lastbound = $firstbound + $this->pagesize - 1;
$tooltip = "showing " . $firstbound . " - " . $lastbound . " records of " . $this->totalrecords . " records";
// First Link
if($this->defaultUrl == "")
$this->defaultUrl = "#";
$paginationlst .= "<li><a id=\"p_1\" href=\"" . $this->defaultUrl . "\" class=\"pagination-css\" data-toggle=\"tooltip\" title=\"" . $tooltip . "\"><i class=\"glyphicon glyphicon-backward\"></i></a></li>\n";
}
$firstbound = (($totalpages - 1) * $this->pagesize);
$lastbound = $firstbound + $this->pagesize - 1;
if ($lastbound > $this->totalrecords)
{
$lastbound = $this->totalrecords;
}
$tooltip = "showing " . $firstbound . " - " . $lastbound . " records of " . $this->totalrecords . " records";
// Previous Link Enabled
if($this->paginationUrl == "")
$this->paginationUrl = "#";
$pid = ($this->pagenumber - 1);
if($pid < 1) $pid = 1;
$prevPageCss = "";
$prevIcon = "<i class=\"glyphicon glyphicon-chevron-left\"></i>";
if($this->paginationstyle == 2)
{
if($this->prevCss != "")
$prevPageCss = " class=\"" . $this->prevCss . "\"";
$prevIcon = "← Previous";
}
$paginationlst .= "<li" . $prevPageCss . "><a id=\"pp_" . $pid . "\" href=\"" . $this->prepareUrl($pid) . "\" data-toggle=\"tooltip\" class=\"pagination-css\" title=\"" . $tooltip . "\">" . $prevIcon . "</a></li>\n";
// Normal Links
if($this->paginationstyle != 2)
$paginationlst .= $this->generate_pagination_links($totalpages, $this->totalrecords, $this->pagenumber, $this->pagesize);
if($this->pagenumber < $totalpages)
{
$paginationlst .= $this->generate_previous_last_links($totalpages, $this->totalrecords, $this->pagenumber, $this->pagesize, $this->showlast);
}
}
else
{
// Normal Links
if($this->paginationstyle != 2)
$paginationlst .= $this->generate_pagination_links($totalpages, $this->totalrecords, $this->pagenumber, $this->pagesize);
// Next Last Links
$paginationlst .= $this->generate_previous_last_links($totalpages, $this->totalrecords, $this->pagenumber, $this->pagesize, $this->showlast);
}
}
$paginationCss = "pagination " . $this->paginationcss;
if($this->paginationstyle == 2)
$paginationCss = "pager";
return "<ul class=\"" . $paginationCss . "\">\n" . $paginationlst . "</ul>\n";
}
function generate_pagination_links($totalpages, $totalrecords, $pagenumber, $pagesize)
{
$script = "";
$firstbound = 0;
$lastbound = 0;
$tooltip = "";
$lst = new pagination();
if($this->paginationstyle == 1)
$arr = $lst->advance_pagination_links($totalpages, $pagenumber);
else
$arr = $lst->simple_pagination_links($totalpages, 15, $pagenumber);
if(count($arr) > 0)
{
foreach ($arr as $item){
$firstbound = (($item - 1) * $pagesize) + 1;
$lastbound = $firstbound + $pagesize - 1;
if ($lastbound > $totalrecords)
$lastbound = $totalrecords;
$tooltip = "showing " . $firstbound . " - " . $lastbound . " records of " . $totalrecords . " records";
$css = "";
if ($item == $pagenumber)
$css = " class=\"active\"";
$script .= "<li" . $css . "><a id=\"pg_" . $item . "\" href=\"" . $this->prepareUrl($item) . "\" class=\"pagination-css\" data-toggle=\"tooltip\" title=\"" . $tooltip . "\">" . $item . "</a></li>\n";
}
}
return $script;
}
function generate_previous_last_links($totalpages, $totalrecords, $pagenumber, $pagesize, $showlast)
{
$script = "";
$firstbound = (($pagenumber) * $pagesize) + 1;
$lastbound = $firstbound + $pagesize - 1;
if ($lastbound > $totalrecords)
$lastbound = $totalrecords;
$tooltip = "showing " . $firstbound . " - " . $lastbound . " records of " . $totalrecords . " records";
// Next Link
$pid = ($pagenumber + 1);
if($pid > $totalpages) $pid = $totalpages;
$nextPageCss = "";
$nextPageIcon = "<i class=\"glyphicon glyphglyphicon glyphicon-chevron-right\"></i>";
if($this->paginationstyle == 2)
{
if($this->nextCss != "")
$nextPageCss = " class=\"" . $this->nextCss . "\"";
$nextPageIcon = "Next →";
}
$script .= "<li" . $nextPageCss . "><a id=\"pn_" . $pid . "\" href=\"" . $this->prepareUrl($pid) . "\" class=\"pagination-css\" data-toggle=\"tooltip\" title=\"" . $tooltip . "\">" . $nextPageIcon . "</a></li>\n";
if ($showlast && $this->paginationstyle != 2)
{
// Last Link
$firstbound = (($totalpages - 1) * $pagesize) + 1;
$lastbound = $firstbound + $pagesize - 1;
if ($lastbound > $totalpages)
$lastbound = $totalpages;
$tooltip = "showing " . $firstbound . " - " . $lastbound . " records of " . $totalrecords . " records";
$script .= "<li><a id=\"pl_" . $totalpages . "\" href=\"" . $this->prepareUrl($totalpages) . "\" class=\"pagination-css\" data-toggle=\"tooltip\" title=\"" . $tooltip . "\"><i class=\"glyphicon glyphicon-forward\"></i></a></li>\n";
}
return $script;
}
function prepareUrl($pid)
{
if($this->paginationUrl == "")
$this->paginationUrl = "#";
if($pid > 1)
return preg_replace("/\[p\]/", $pid, $this->paginationUrl);
else
return preg_replace("/\[p\]/", $pid, $this->defaultUrl);
}
}
?>