-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathTableModel.php
More file actions
130 lines (115 loc) · 3.02 KB
/
Copy pathTableModel.php
File metadata and controls
130 lines (115 loc) · 3.02 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
124
125
126
127
128
129
130
<?php
class TableModel {
public $data;
public $table ="events";
public function __construct($tbl="")
{
if($tbl){
$this->table = $tbl;
}
}
public function get_connection(){
$dsn = "mysql:host=localhost;dbname=wftutorials";
$user = "root";
$passwd = "";
$conn = new PDO($dsn, $user, $passwd);
return $conn;
}
private function getTableColumns(){
return $this->cols;
}
public function getResults($results, $sel=array()){
$data = [];
foreach ($results as $row){
$line = [];
foreach($row as $key=>$value){
if(is_numeric($key)){
continue;
}
if(count($sel) > 0){
if(in_array($key, $sel)){
//echo $value;
$line[$key] = $value;
}
}else{
$line[$key] = $value;
}
}
$data[] = $line;
}
return $data;
}
public function getAllRecords($sel=array()){
$conn = $this->get_connection();
$query = $conn->prepare("SELECT * from ". $this->table);
$query->execute([]);
$results = $query->fetchAll();
return $this->getResults($results, $sel);
}
public function getOneRecordByPk($id, $pk='id'){
$conn = $this->get_connection();
$query = $conn->prepare("SELECT * from ". $this->table. " WHERE $pk=? LIMIT 1");
$query->execute([$id]);
$results = $query->fetchAll();
return $this->getResults($results)[0];
}
private function getInsertSQL($data){
$sql = "INSERT INTO ". $this->table;
$sql .= " (";
foreach ($data as $col){
$sql .= "`$col`,";
}
$sql = rtrim($sql,',');
$sql .= ") VALUES (";
foreach ($data as $col){
$sql .= "?,";
}
$sql = rtrim($sql,',');
$sql .= ")";
return $sql;
}
private function getUpdateSQL($data, $pk){
$sql = "UPDATE ". $this->table . " SET ";
$sql .= "";
foreach ($data as $col){
$sql .= "`$col`=?,";
}
$sql = rtrim($sql,',');
$sql .= " WHERE `$pk`=?";
return $sql;
}
private function getColsAndValues($data){
$cols = [];
$values = [];
foreach ($data as $key=> $value){
$cols[] = $key;
$values[] = $value;
}
return [
'cols' => $cols,
'values' => $values
];
}
public function insertRecord($data=[]){
$conn = $this->get_connection();
$cvs = $this->getColsAndValues($data);
$sql = $this->getInsertSQL($cvs['cols']);
$query = $conn->prepare($sql); // prepare
$query->execute($cvs['values']); // execute
return true;
}
public function updateRecordByPk($id, $data=[], $pk='id'){
$conn = $this->get_connection();
$cvs = $this->getColsAndValues($data);
$sql = $this->getUpdateSQL($cvs['cols'], $pk);
$query = $conn->prepare($sql); // prepare
$res = array_merge($cvs['values'],[$id]);
$query->execute($res); // execute
}
public function deleteRecordByPk($id, $pk="id"){
$conn = $this->get_connection();
$query = $conn->prepare("DELETE FROM ".$this->table." WHERE $pk=?");
$query->execute([$id]);
return true;
}
}