-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable.php
102 lines (90 loc) · 2.59 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
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
<?php
/*
* Created by PhpStorm.
* User: DiniX
* Date: 27-Jun-17
* Time: 10:05 AM
*/
abstract class table
{
protected $id = null;
protected $tableName = null;
function __construct()
{
}
protected function buildQuery($task)
{
$sql = '';
if ($task == 'load') {
$sql = "SELECT * from {$this->tableName} where id = '{$this->id}'";
return $sql;
} elseif ($task == 'insert') {
$keys = "";
$values = "";
$classAttributes = get_class_vars(get_class($this));
$sql .= "INSERT into {$this->tableName} ";
foreach ($classAttributes as $key => $value) {
if ($key == "tableName") {
continue;
}
$keys .= "{$key},";
$values .= "'{$this->$key}',";
}
$sql .= "(" . substr($keys, 0, -1) . ") Values(" . substr($values, 0, -1) . ")";
return $sql;
} elseif ($task == 'update') {
$classAttributes = get_class_vars(get_class($this));
$sql .= "UPDATE {$this->tableName} set ";
foreach ($classAttributes as $key => $value) {
if ($key == "id" || $key == "tableName") {
continue;
}
$sql .= "{$key} = '{$this->$key}',";
}
$sql = substr($sql, 0, -1) . " where id = '{$this->id}'";
return $sql;
}
}
function load($dbObj, $id)
{
$this->id = $id;
//$dbObj = database::getInstance();
$sql = $this->buildQuery('load');
$dbObj->doQuery($sql);
if(mysqli_num_rows($dbObj->getResult())>0) {
$rows = $dbObj->loadObjList();
foreach ($rows as $key => $value) {
if ($key == 'id') {
continue;
}
$this->$key = $value;
}
return true;
}else{
return false;
}
}
function insert($dbObj)
{
//$dbObj = database::getInstance();
$sql = $this->buildQuery('insert');
$dbObj->doQuery($sql);
}
function update($dbObj)
{
//$dbObj = database::getInstance();
$sql = $this->buildQuery('update');
$dbObj->doQuery($sql);
}
function featuredLoad($dbObj,$sql){
$result = mysqli_query($dbObj->getConnection(),$sql) or die("Database access failed..!!");
return $result;
}
function bind($data)
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
}
?>