-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.php
87 lines (76 loc) · 2.75 KB
/
class.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
<?php
class DB extends Mysqli{
// conection DB data
private $host = "us-cdbr-east-05.cleardb.net";
private $db_user = "bbe11075ea0456";
private $db_pass = "33e74393";
private $db_name = "heroku_383bbb66ca7b997";
//Overall data
private $method;
private $data;
private $id;
public function __construct($method, $data, $id = false){
$this -> method = $method;
$this -> data = $data;
$this -> id = $id;
parent:: __construct(
$this -> host,
$this -> db_user,
$this -> db_pass,
$this -> db_name
);
switch($this -> method){
case 'POST':
echo( $this -> add());
break;
case 'GET':
echo $this -> get();
break;
case 'PUT':
echo $this -> update();
break;
case 'DELETE':
// $product_to_delete = $_GET['id'];
echo $this -> delete();
break;
}
}
private function add(){
$json_data = json_decode($this -> data);
$resul['message'] = $this -> Query("INSERT INTO products
(name,
description,
quantity,
price)
VALUES(
'$json_data->name',
'$json_data->description',
$json_data->quantity,
$json_data->price)");
return json_encode($resul);
}
private function get(){
// $resul['message'] = "GET works!";
$resul = $this -> Query("SELECT * FROM products");
$products = [];
while($row = mysqli_fetch_array($resul)){
$newproduct["id"] = $row['id'];
$newproduct["name"] = $row['name'];
$newproduct["description"] = $row['description'];
$newproduct["quantity"] = $row['quantity'];
$newproduct["price"] = $row['price'];
array_push($products, $newproduct);
}
return json_encode($products);
}
private function update(){
$json_data = json_decode($this->data);
$resul['message'] = $this -> Query("UPDATE products SET name='$json_data->name',description='$json_data->description',quantity='$json_data->quantity',price='$json_data->price' WHERE id = $this->id");
return json_encode($resul);
}
private function delete(){
$resul['message'] = $this -> Query("DELETE FROM products WHERE id = $this->id");
return json_encode($resul);
}
}
?>