-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-php-functions.php
224 lines (169 loc) · 6.14 KB
/
gen-php-functions.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
include_once "db-connect.php";
include_once "mysql-exe-functions.php";
echo "// Generated : ";
echo create_now_timestamp();
echo "<br><br>";
// Get a list of all the tables in the DB
$mysql = "SHOW TABLES";
$tmp = "Tables_in_" . $db;
$index = array($tmp);
$mySQL_data = query2D($pdo,$mysql,$index);
$tables = $mySQL_data['data'];
$tmp_array_index = "Tables_in_$db";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Required meta tags -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;">
<title></title>
<style>
.db_table_function{
}
.hidden{
display: none;
}
</style>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function(){
$("#table_dropdown").change(function (e) {
e.preventDefault();
selected_table = $(this).val();
console.log(selected_table);
if (selected_table == "show_all") {
$(".db_table_function").removeClass("hidden");
} else {
$(".db_table_function").addClass("hidden");
$("#code_"+selected_table).removeClass("hidden");
}
});
});
</script>
</head>
<body>
<select id="table_dropdown" class="form-control">
<option value="show_all"> SHOW ALL </option>
<?php for ($i=0; $i < sizeof($tables); $i++) { ?>
<option value="<?php echo $tables[$i][$tmp_array_index]; ?>"> <?php echo $tables[$i][$tmp_array_index]; ?> </option>
<?php } ?>
</select>
<?php
// Go through each table
for ($i=0; $i < sizeof($tables); $i++) {
$myTable = $tables[$i][$tmp_array_index];
$index = array("Field","Type");
$mysql = "DESCRIBE " . $myTable;
$mySQL_data = query2D($pdo,$mysql,$index);
$table_struct = $mySQL_data['data'];
if ($i % 2 == 0) {
echo "<div id='code_$myTable' class='db_table_function' style='background-color: #FAD7A0;'>";
} else {
echo "<div id='code_$myTable' class='db_table_function' style='background-color: #F9E79F;'>";
}
// Create index list code
// $index_list = array("network_print","network_files");
$tmp = "";
for ($j=0; $j < sizeof($table_struct); $j++) {
$tmp .= '"' . $table_struct[$j]['Field'] . '"';
$tmp .= ",";
}
$tmp = substr($tmp,0,-1); // Get rid of the last comma
$index_list_str = "\$index_list = array($tmp);";
// Create
echo "<h1> TABLE : $myTable </h1>";
// queryExist
echo "<h2> queryExist ($myTable) </h2>";
echo "<h3>Query if a certain value exist in the DB </br>";
echo "\$return_data['data']['exist'] will either return true or false</h3>";
$tmp = "SELECT [var1] FROM $myTable WHERE [var2]";
echo "$tmp <br>";
$tmp = "\$mySQL_return = queryExist(\$pdo,\$mysql);";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
// query1
echo "<h2> query1 ($myTable) </h2>";
echo "<h3>Query the DB and return a 1 value </br>";
echo "This function is used mainly when you are looking for just 1 particular value</h3>";
$tmp = "\$mysql = \"SELECT [var1] FROM $myTable WHERE [var2]\";";
echo "$tmp <br>";
$tmp = "\$mySQL_return = query1(\$pdo,\$mysql,\"[var1]\");";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
// query1D
echo "<h2> query1D ($myTable) </h2>";
echo "<h3>Query the DB and return a 1 dimensional array </br>";
echo "This function is used usually when you have only 1 row to return from the database but with multiple columns<br>The function will only return the first row</h3>";
echo $index_list_str;
echo "<br>";
$tmp = "\$mysql = \"SELECT * FROM $myTable WHERE [var1]\";";
echo "$tmp <br>";
$tmp = "\$mySQL_return = query1D(\$pdo,\$mysql,\$index_list);";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
// query2D
echo "<h2> query2D ($myTable) </h2>";
echo "<h3>Query the DB and return a 2 dimensional array </br>";
echo "This function is used usually when you have more than 1 row to return from the database but with multiple columns</h3>";
echo $index_list_str;
echo "<br>";
$tmp = "\$mysql = \"SELECT * FROM $myTable WHERE [var1]\";";
echo "$tmp <br>";
$tmp = "\$mySQL_return = query2D(\$pdo,\$mysql,\$index_list);";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
// insert_row
echo "<h2> insert_row ($myTable) </h2>";
echo "<h3>Insert values into the database </h3>";
echo "\$dbTable = \"$myTable\";";
echo "<br>";
echo "\$mysql_insert = array();";
echo "<br><br>";
for ($j=0; $j < sizeof($table_struct); $j++) {
echo "\$mysql_insert['" . $table_struct[$j]['Field'] . "'] = $" . $table_struct[$j]['Field'] . ";";
echo "<br>";
}
echo "<br>";
$tmp = "\$mySQL_return = insert_row(\$pdo,\$dbTable,\$mysql_insert);";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
// update_row
echo "<h2> update_row ($myTable) </h2>";
echo "<h3>Update values into the database </h3>";
echo "\$dbTable = \"$myTable\";";
echo "<br>";
echo "\$where_clause = \"\";";
echo "<br>";
echo "\$mysql_vars = array();";
echo "<br><br>";
for ($j=0; $j < sizeof($table_struct); $j++) {
echo "\$mysql_vars['" . $table_struct[$j]['Field'] . "'] = ;";
echo "<br>";
}
echo "<br>";
$tmp = "\$mySQL_return = update_row(\$pdo,\$dbTable,\$mysql_vars,\$where_clause);";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
// delete
echo "<h2> delete_row ($myTable) </h2>";
echo "<h3>Delete from the DB </h3>";
$tmp = "\$mysql = \"DELETE FROM $myTable WHERE [var1]\";";
echo "$tmp <br>";
$tmp = "\$mySQL_return = delete_row(\$pdo,\$mysql);";
echo "$tmp <br>";
$tmp = "return \$mySQL_return;";
echo "$tmp <br>";
echo "</div>";
}
?>
</body>
</html>