-
Notifications
You must be signed in to change notification settings - Fork 2
/
r-crud.php
144 lines (86 loc) · 3.27 KB
/
r-crud.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
// R-CRUD ( Rusher CRUD ) - Fast & Instant CRUD
// Copyright ©2021 - Afrizal F.A - ICWR-TEAM
class r_crud
{
public $connection;
public function __construct($connection)
{
$this->connection = $connection;
}
public function data_create($table, $data)
{
$table = mysqli_real_escape_string($this->connection, $table);
$index = implode(", ", array_map(function ($v, $k) { unset($v); return $k; }, $data, array_keys($data)));
$value = implode(", ", array_map(function ($v, $k) { unset($k); return "\"$v\""; }, $data, array_keys($data)));
$query = "INSERT INTO $table ($index) VALUES ($value)";
$execute = mysqli_query($this->connection, $query);
if ($execute) {
return true;
} else {
return false;
}
}
public function data_read_all($table)
{
$table = mysqli_real_escape_string($this->connection, $table);
$query = "SELECT * FROM $table";
$execute = mysqli_query($this->connection, $query);
$data = [];
$no = 1;
if ($execute) {
while ($raw_data = mysqli_fetch_array($execute)) {
$data[$no++] = $raw_data;
}
return $data;
} else {
return false;
}
}
public function data_read_where($table, $where)
{
if (!empty($where)) {
$table = mysqli_real_escape_string($this->connection, $table);
$structure_where = implode("AND ", array_map(function ($v, $k) { return "$k = \"$v\""; }, $where, array_keys($where)));
$query = "SELECT * FROM $table WHERE $structure_where";
$execute = mysqli_query($this->connection, $query);
if ($execute) {
if (mysqli_num_rows($execute) > 0) {
$data = mysqli_fetch_array($execute);
return $data;
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
}
public function data_update($table, $data, $where)
{
$table = mysqli_real_escape_string($this->connection, $table);
$struct_data = implode(", ", array_map(function ($v, $k) { return "$k = \"$v\""; }, $data, array_keys($data)));
$structure_where = implode("AND ", array_map(function ($v, $k) { return "$k = \"$v\""; }, $where, array_keys($where)));
$query = "UPDATE $table SET $struct_data WHERE $structure_where";
$execute = mysqli_query($this->connection, $query);
if ($execute) {
return true;
} else {
return false;
}
}
public function data_delete($table, $where)
{
$table = mysqli_real_escape_string($this->connection, $table);
$structure_where = implode("AND ", array_map(function ($v, $k) { return "$k = \"$v\""; }, $where, array_keys($where)));
$query = "DELETE FROM $table WHERE $structure_where";
$execute = mysqli_query($this->connection, $query);
if ($execute) {
return true;
} else {
return false;
}
}
}