-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.php
74 lines (58 loc) · 1.67 KB
/
Table.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
<?php
require_once("Database.php");
class Table {
var $db;
var $table;
var $selection;
var $where;
var $group;
function __construct (Database $db, $table) {
$this->db = $db;
$this->table = array($table);
$this->defaults();
}
/** Chainable method. */
function defaults () {
$this->selection = array();
$this->where = array('1');
$this->group = array();
return $this;
}
private function add (&$array, $value) {
if (!in_array($value, $array)) {
$array[] = $value;
}
return $this;
}
/** Chainable method. */
function select ($value = "*") {
return $this->add($this->selection, $value);
}
/** Chainable method. */
function where ($value) {
return $this->add($this->where, $value);
}
/** Chainable method. */
function group ($value) {
return $this->add($this->group, $value);
}
function getQuery () {
$what = "*";
if (count($this->selection)!=0) $what = implode($this->selection, ',');
$tables = implode($this->table, ',');
$where = implode($this->where, ' AND ');
$group = null;
if (count($this->group)!=0) $group = implode($this->group, ',');
$query = "SELECT $what FROM $tables WHERE $where";
if ($group != null) $query .= " GROUP BY $group";
return $query;
}
/** Chainable method. */
function get ($callback) {
$query = $this->getQuery();
foreach ($this->db->getPDO()->query($query) as $row) {
call_user_func($callback, $row);
}
return $this->defaults();
}
}