1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Pagination Class |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class Pagination { |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @access public |
14
|
|
|
* @var integer Current Page |
15
|
|
|
*/ |
16
|
|
|
public $currentPage; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @access public |
20
|
|
|
* @var integer Number of items(newsfeed, posts, ..etc.) to be displayed per page |
21
|
|
|
*/ |
22
|
|
|
public $perPage; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @access public |
26
|
|
|
* @var integer Total count of items(newsfeed, posts, ..etc.) |
27
|
|
|
*/ |
28
|
|
|
public $totalCount; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This is the constructor for Pagination Object. |
32
|
|
|
* |
33
|
|
|
* @access public |
34
|
|
|
* @param integer $currentPage |
35
|
|
|
* @param integer $totalCount |
36
|
|
|
* @param integer $perPage Number of items per page |
37
|
|
|
*/ |
38
|
|
|
public function __construct($currentPage = 1, $totalCount = 0, $perPage = 0){ |
39
|
|
|
$this->currentPage = (empty($currentPage))? 1: (int)$currentPage; |
40
|
|
|
$this->totalCount = (empty($totalCount))? 0: (int)$totalCount; |
41
|
|
|
$this->perPage = (empty($perPage))? Config::get('PAGINATION_DEFAULT_LIMIT'): (int)$perPage; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* get pagination object by executing COUNT(*) query. |
46
|
|
|
* |
47
|
|
|
* @access public |
48
|
|
|
* @param string $table |
49
|
|
|
* @param string $options |
50
|
|
|
* @param array $values array of data |
51
|
|
|
* @param integer $pageNum |
52
|
|
|
* @param integer $extraOffset check comment class |
53
|
|
|
* @return Pagination |
54
|
|
|
*/ |
55
|
|
|
public static function pagination($table, $options, $values, $pageNum, $extraOffset = 0){ |
56
|
|
|
|
57
|
|
|
$database = Database::openConnection(); |
58
|
|
|
$query = "SELECT COUNT(*) AS count FROM {$table} "; |
59
|
|
|
$query .= $options; |
60
|
|
|
|
61
|
|
|
$database->prepare($query); |
62
|
|
|
$database->execute($values); |
63
|
|
|
$totalCount = $database->fetchAssociative()["count"]; |
64
|
|
|
$extraOffset = ((int)$extraOffset > $totalCount)? 0: (int)$extraOffset; |
65
|
|
|
|
66
|
|
|
return new Pagination((int)$pageNum, $totalCount - $extraOffset); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the offset. |
71
|
|
|
* |
72
|
|
|
* @access public |
73
|
|
|
* @return integer |
74
|
|
|
*/ |
75
|
|
|
public function getOffset() { |
76
|
|
|
return ($this->currentPage - 1) * $this->perPage; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get number of total pages. |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* @return integer |
84
|
|
|
*/ |
85
|
|
|
public function totalPages() { |
86
|
|
|
return ceil($this->totalCount/$this->perPage); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the number of the previous page. |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
* @return integer Number of previous page |
94
|
|
|
*/ |
95
|
|
|
public function previousPage() { |
96
|
|
|
return $this->currentPage - 1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the number of the next page. |
101
|
|
|
* |
102
|
|
|
* @access public |
103
|
|
|
* @return integer Number of next page |
104
|
|
|
*/ |
105
|
|
|
public function nextPage() { |
106
|
|
|
return $this->currentPage + 1; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* checks if there is a previous page or not |
111
|
|
|
* |
112
|
|
|
* @access public |
113
|
|
|
* @return boolean |
114
|
|
|
*/ |
115
|
|
|
public function hasPreviousPage() { |
116
|
|
|
return $this->previousPage() >= 1 ? true : false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* checks if there is a next page or not |
121
|
|
|
* |
122
|
|
|
* @access public |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
public function hasNextPage() { |
126
|
|
|
return $this->totalPages() >= $this->nextPage()? true : false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.