Skip to content

Commit b1479cc

Browse files
perf: store json type data
1 parent 8bc0764 commit b1479cc

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

models/get.model.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
<?php
1+
<?php
22

33
/* GET Class */
44
class GetModel {
5-
5+
66
function __construct() { }
77

88
/* GET All Data */
99
function getData($table, $id) {
10-
10+
1111
/* Check ID */
1212
if(!isset($id)) { $where = ''; }
13-
else { $where = "WHERE id = $id"; }
14-
13+
else { $where = "WHERE id = $id"; }
14+
1515
/* Include Database File */
1616
include 'views/database.view.php';
17-
17+
1818
/* Get All Data */
1919
$sql = "SELECT * FROM $table $where";
2020
$result = mysqli_query($con, $sql);
2121

22+
while ($row = mysqli_fetch_assoc($result)) {
23+
$arr[] = $row;
24+
}
25+
26+
foreach ($arr[0] as $key => $val) {
27+
28+
// String convert to Array
29+
if (strpos($val, "[{") === 0) {
30+
$arr[0][$key] = json_decode($val, true);
31+
}
32+
}
33+
2234
if (mysqli_num_rows($result) > 0) {
23-
echo json_encode(array('status'=>'Success', 'data'=>mysqli_fetch_all($result, MYSQLI_ASSOC)));
35+
echo json_encode(array('status'=>'Success', 'data'=>$arr));
2436
}
2537
else {
2638
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));

models/post.model.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
1-
<?php
1+
<?php
22

33
/* POST Class */
44
class PostModel {
5-
5+
66
function __construct() { }
77

88
/* POST Data */
99
function postData($table, $data) {
10-
10+
1111
/* Check Data */
12-
if(!isset($data)) {
12+
if(!isset($data)) {
1313
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
1414
die();
15-
}
15+
}
1616

1717
/* Include Database File */
1818
include 'views/database.view.php';
1919

2020
/* Insert Data */
21-
$data = json_decode($data, true);
21+
$data = json_decode($data, true);
2222
$keys = implode(",", array_keys($data));
23-
$vals = implode("','", array_values($data));
24-
25-
$sql = "INSERT INTO $table ($keys) VALUES ('$vals')";
23+
$vals = array();
24+
25+
foreach (array_values($data) as $value) {
26+
27+
// Array convert to String
28+
if (is_array($value)) {
29+
$value = json_encode($value);
30+
}
31+
32+
// Value Push in Array
33+
array_push($vals, html_entity_decode($value));
34+
}
35+
36+
$val = implode("','", $vals);
37+
38+
$sql = "INSERT INTO $table ($keys) VALUES ('$val')";
2639

2740
if ($con->query($sql) === TRUE) {
2841
echo json_encode(array('status'=>'Success', 'message'=>'Data is Inserted.'));
29-
}
42+
}
3043
else {
3144
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
3245
die();

models/put.model.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1-
<?php
1+
<?php
22

33
/* PUT Class */
44
class PutModel {
5-
5+
66
function __construct() { }
77

88
/* PUT Data */
99
function putData($table, $id, $data) {
1010

1111
/* Check Data and Check ID */
12-
if(!isset($data) && !isset($id)) {
13-
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
14-
die();
15-
}
12+
if(!isset($data) && !isset($id)) {
13+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
14+
die();
15+
}
1616

1717
/* Include Database File */
1818
include 'views/database.view.php';
1919

2020
/* Update Data */
2121
$data = json_decode($data, true);
22-
$dataString = '';
22+
$dataString = '';
2323

2424
foreach($data as $key=>$value) {
25-
$dataString = $dataString."$key = '$value', ";
25+
26+
// Array convert to String
27+
if (is_array($value)) {
28+
$value = json_encode($value);
29+
}
30+
31+
$dataString = $dataString."$key = '$value', ";
2632
}
2733

2834
$dataString = substr($dataString, 0, -2);
2935

3036
$sql = "UPDATE $table SET $dataString WHERE id = $id";
3137

3238
if ($con->query($sql) === TRUE) {
33-
echo json_encode(array('status'=>'Success', 'message'=>'Data is Updated.'));
34-
}
39+
echo json_encode(array('status'=>'Success', 'message'=>'Data is Updated.'));
40+
}
3541
else {
36-
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
37-
die();
42+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
43+
die();
3844
}
3945
}
4046
}

0 commit comments

Comments
 (0)