-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
70 lines (66 loc) · 2.15 KB
/
cart.php
File metadata and controls
70 lines (66 loc) · 2.15 KB
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
<?php
function cartAdd(array $fields) : bool {
$sql = "INSERT INTO cart (`food_id`, `name`, `count`, `user_id`) VALUES (:food_id, :name, :count, :id)";
$params = [
':food_id' => $fields['food_id'],
':name' => $fields['name'],
':count' => $fields['count'],
':id' => $fields['id'],
];
dbQuery($sql, $fields);
return true;
}
function cartEdit(array $fields) : bool{
$sql = "UPDATE cart SET food_id = :food_id, name = :name, count = :count, user_id = :id WHERE food_id = :food_id AND user_id = :id";
$params = [
':food_id' => $fields['food_id'],
':name' => $fields['name'],
':count' => $fields['count'],
':id' => $fields['id'],
];
dbQuery($sql, $fields);
return true;
}
function cartAddCount(array $fields) : bool {
$sql = "INSERT INTO cart (`food_id`, `count`, `user_id`) VALUES (:food_id, :count, :id)";
$params = [
':food_id' => $fields['food_id'],
':count' => $fields['count'],
':id' => $fields['id'],
];
dbQuery($sql, $fields);
return true;
}
function cartDelete(array $fields) : bool{
$sql = "DELETE FROM cart WHERE user_id = :id";
dbQuery($sql, $fields);
return true;
}
function cartAll(array $fields) : array{
$sql = "SELECT * FROM cart WHERE user_id = :user_id";
$query = dbQuery($sql, $fields);
return $query->fetchAll();
}
function cartOne(array $fields) : array{
$sql = "SELECT * FROM cart WHERE food_id = :food_id AND user_id = :id";
$query = dbQuery($sql, $fields);
return $query->fetchAll()[0];
}
function checkCartExists($id) {
$sql = "SELECT COUNT(*) FROM cart WHERE food_id = :food_id";
$query = dbQuery($sql, [':food_id' => $id]);
$result = $query->fetchColumn();
return $result > 0;
}
function convertToCartItemFormat($jsonString) {
$items = $jsonString;
$cartItems = [];
foreach ($items as $item) {
$cartItem = [
'id' => $item['food_id'],
'count' => $item['count']
];
$cartItems[] = $cartItem;
}
return $cartItems;
}