-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination-class.php
52 lines (45 loc) · 1.6 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
<?php
class Pagination{
private $db, $table, $total_records;
public $start=0,$limit = 12;
//PDO connection
public function __construct($table){
$this->table = $table;
$this->db = new PDO("mysql:host=localhost; dbname=arish-store", "root", "");
$this->set_total_records();
}
public function set_total_records(){
$stmt = $this->db->prepare("SELECT id FROM $this->table");
$stmt->execute();
$this->total_records = $stmt->rowCount();
}
public function current_page(){
return isset($_GET['page']) ? (int)$_GET['page'] :1;
}
public function get_data($data){
$start = 1;
if($this->current_page() > 1){
$start = ($this->current_page() * $this->limit) - $this->limit;
}
$stmt = $this->db->prepare("SELECT categories.ID as cat_id,categories.Name as cat_name, posts.*
FROM $this->table
INNER JOIN categories
ON posts.Cat_ID = categories.ID
WHERE Cat_ID = $data
order by Post_ID DESC
LIMIT $start, $this->limit
");
$stmt->execute(array($data));
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
public function get_pagination_number($data){
if($this->current_page() > 1){
$start = ($this->current_page() * $this->limit) - $this->limit;
}
$stmt = $this->db->prepare("SELECT count(*) as count FROM $this->table WHERE Cat_ID = $data order by Post_ID DESC LIMIT $this->start, $this->limit");
$stmt->execute();
$the_stmt = $stmt->fetchAll(PDO::FETCH_OBJ);
return json_decode(json_encode($the_stmt), true);
}
}
?>