1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace database; |
5
|
|
|
|
6
|
|
|
use database\DB; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Read |
10
|
|
|
* @package src |
11
|
|
|
*/ |
12
|
|
|
class Read |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
private $db; |
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
private $table; |
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
private $read; |
27
|
|
|
/** |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
private $statement; |
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private $terms; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var |
39
|
|
|
*/ |
40
|
|
|
private $query; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
private $limit; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var |
49
|
|
|
*/ |
50
|
|
|
private $offset; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var |
54
|
|
|
*/ |
55
|
|
|
private $page; |
56
|
|
|
/** |
57
|
|
|
* @var |
58
|
|
|
*/ |
59
|
|
|
private $link; |
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var |
62
|
|
|
*/ |
63
|
|
|
private $rows; |
64
|
|
|
/** |
65
|
|
|
* @var |
66
|
|
|
*/ |
67
|
|
|
private $maxLinks; |
68
|
|
|
/** |
69
|
|
|
* @var |
70
|
|
|
*/ |
71
|
|
|
private $first; |
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var |
74
|
|
|
*/ |
75
|
|
|
private $last; |
|
|
|
|
76
|
|
|
|
77
|
|
|
private $paginator; |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var |
81
|
|
|
*/ |
82
|
|
|
private $result; |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Read constructor. |
87
|
|
|
*/ |
88
|
|
|
public function __construct() |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getResult() |
97
|
|
|
{ |
98
|
|
|
return $this->result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $table |
103
|
|
|
* @param null $parse |
|
|
|
|
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
|
107
|
|
|
public function query(string $query, string $parse = null) |
108
|
|
|
{ |
109
|
|
|
if ($parse) { |
110
|
|
|
parse_str($parse, $this->statement); |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->query = $query; |
115
|
|
|
$this->read = DB::connect()->prepare($this->query); |
116
|
|
|
$this->read->execute($this->statement); |
117
|
|
|
$this->rows = $this->read->rowCount(); |
118
|
|
|
$this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
119
|
|
|
return $this; |
|
|
|
|
120
|
|
|
|
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $table |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function all($table) |
129
|
|
|
{ |
130
|
|
|
$this->table = (string)$table; |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->read = DB::connect()->prepare("SELECT * FROM {$this->table}"); |
134
|
|
|
$this->read->execute(); |
135
|
|
|
$this->rows = $this->read->rowCount(); |
136
|
|
|
return $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
|
|
|
|
137
|
|
|
|
138
|
|
|
return $this; |
|
|
|
|
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $table |
144
|
|
|
* @param string $terms |
145
|
|
|
* @param string $parse |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function find(string $table, string $terms, string $parse) |
149
|
|
|
{ |
150
|
|
|
$this->table = $table; |
151
|
|
|
$this->terms = $terms; |
152
|
|
|
|
153
|
|
|
if ($parse) { |
154
|
|
|
parse_str($parse, $this->statement); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
$this->read = DB::connect()->prepare("SELECT * FROM {$this->table} {$this->terms}"); |
159
|
|
|
$this->read->execute($this->statement); |
160
|
|
|
$this->rows = $this->read->rowCount(); |
161
|
|
|
return $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
162
|
|
|
} catch (\PDOException $e) { |
163
|
|
|
echo $e->getMessage() . " in " . $e->getFile(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function getRowCount() |
173
|
|
|
{ |
174
|
|
|
return $this->rows; |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param int|null $pag |
180
|
|
|
* @param int $limit |
181
|
|
|
*/ |
182
|
|
|
public function page(int $pag = null, int $limit, int $totalData) |
183
|
|
|
{ |
184
|
|
|
$this->page = ($pag ? $pag : 1); |
185
|
|
|
$this->limit = $limit; |
186
|
|
|
$this->offset = ($this->page * $this->limit) - $this->limit; |
187
|
|
|
$this->maxLinks = ceil($totalData / $this->limit); |
188
|
|
|
|
189
|
|
|
try { |
190
|
|
|
|
191
|
|
|
$this->read = DB::connect()->prepare("SELECT * FROM {$this->table} LIMIT {$this->limit} OFFSET {$this->offset}"); |
192
|
|
|
$this->read->execute(); |
193
|
|
|
$this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
} catch (\PDOException $e) { |
197
|
|
|
echo $e->getMessage() . " in " . $e->getFile(); |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function render() |
205
|
|
|
{ |
206
|
|
|
for ($i = 1; $i <= $this->maxLinks; $i++){ |
207
|
|
|
echo "<a href=\"?atual={$i}\" class=\"paginator\">{$i}</a>"; |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
} |