This repository was archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyclPagination.php
More file actions
123 lines (101 loc) · 2.42 KB
/
yclPagination.php
File metadata and controls
123 lines (101 loc) · 2.42 KB
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
<?php
/**
* Pagination Helper
*
* @author Popsana Barida
* @link https://yeastycode.com YeastyCode
*/
class yclPagination {
/**
* @var type int Number of pages.
*/
public $no_pages;
/**
* @var type int Next Page
*/
public $next_page;
/**
* @var int Previous page
*/
public $prev_page;
/**
*
* @var int Current Page
*/
public $current_page;
private $db_start_point;
private $total_no_items;
private $max_no_rows;
/**
*
* @param type $max_no_rows The number of rows you want to display at a time.
* @param type $current_page
*/
function __construct($max_no_rows, $current_page = 1) {
$this->current_page = floor($current_page) <= 0 ? 1 : floor($current_page);
$this->max_no_rows = $max_no_rows;
}
/**
*
* @param type $total_no_items The total number of items in the database
*/
function setItemCount($total_no_items) {
$this->total_no_items = $total_no_items;
$this->setNoPages();
}
/**
* Set the number of pages available..
*/
private function setNoPages() {
$div_raw = $this->total_no_items / $this->max_no_rows;
$floored = ceil($div_raw);
$difference = $div_raw - $floored;
$this->no_pages = ($difference > 0) ? $floored++ : $floored;
$this->setNav();
}
/**
* Compute Nagivation
*/
private function setNav() {
$this->next_page = ($this->current_page >= $this->no_pages) ? $this->no_pages : ($this->current_page + 1);
$this->prev_page = ($this->current_page > 1) ? ($this->current_page - 1) : 1;
$this->setDBStart();
}
/**
* Set the database's start point (for LIMIT)
*/
private function setDBStart() {
$this->db_start_point = ($this->current_page * $this->max_no_rows) - $this->max_no_rows;
}
/**
*
* @return int DB Start Point
*/
public function getDBStartPoint() {
return floor($this->db_start_point);
}
private $query = "";
/**
*
* @param type $query
* @return string Your original sql query
*/
function setFullQuery($query) {
$this->query = $query;
return $query;
}
private $select_query = "";
/**
*
* @param type $total_no_items total number of for this query
* @return string Your sql query with limit
*/
function getSelectQuery($total_no_items) {
$this->setItemCount($total_no_items);
$f1 = str_replace(";", "", $this->query);
$f2 = preg_replace("/^LIMIT.+/", "", $f1);
$f3 = $f2 . " LIMIT {$this->getDBStartPoint()},{$this->max_no_rows};";
$this->select_query = $f3;
return $f3;
}
}