-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
105 lines (97 loc) · 2.41 KB
/
cart.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
<?php
/*
* File: cart.php
* Project: Project 4
* Author: Hayden Kowalchuk
* -----
* Copyright (c) 2021 Hayden Kowalchuk, Hayden Kowalchuk
* License: BSD 3-clause "New" or "Revised" License, http://www.opensource.org/licenses/BSD-3-Clause
*/
function cart_add($id)
{
/* Check if found an increment, otherwise add new */
$found = false;
foreach ($_SESSION['cart'] as $key => $val) {
if ($val['id'] == $id) {
$found = $key;
break;
}
}
if ($found !== false) {
/* found, increment */
$_SESSION['cart'][$found]['num'] = intval($_SESSION['cart'][$found]['num']) + 1;
} else {
/* not found */
$el = array("id" => $id, "num" => 1);
array_push($_SESSION['cart'], $el);
}
}
function cart_remove($id)
{
/* Check if found an increment, otherwise add new */
$found = false;
foreach ($_SESSION['cart'] as $key => $val) {
if ($val['id'] == $id) {
$found = $key;
break;
}
}
if ($found !== false) {
/* found, remove */
array_splice($_SESSION['cart'], $found, 1);
}
}
function cart_clear()
{
$_SESSION['cart'] = array();
}
function cart_get()
{
return $_SESSION['cart'];
}
function cart_num()
{
return count(cart_get());
}
function cart_place_order($total_cost)
{
global $link;
$cart_string = serialize($_SESSION['cart']);
/* Setup location inventory */
$insert_order = 'INSERT INTO orders (user_id, cart, price, date_start, date_end, city) VALUES (?,?,?,?,?,?)';
mysqli_select_db($link, DB_DATABASE);
$stmt_city = mysqli_prepare($link, $insert_order);
if (!$stmt_city) {
die("ERROR: Could not prepare order. " . mysqli_error($link));
}
if (!mysqli_stmt_bind_param(
$stmt_city,
'isiiis',
$_SESSION['id'],
$cart_string,
$total_cost,
$_SESSION['reserve_start'],
$_SESSION['reserve_end'],
$_SESSION['city']
)) {
die("ERROR: Could not bind order. " . mysqli_error($link));
return;
}
if (!mysqli_stmt_execute($stmt_city)) {
die("ERROR: Could not add order. " . mysqli_error($link));
return;
}
return mysqli_insert_id($link);
}
function order_update_stock($city, $id)
{
global $link;
/* Decrement available stock */
$inv_update = "UPDATE " . $city . " SET available=available-1 WHERE id={$id} AND available>0";
if ($stmt_up = mysqli_prepare($link, $inv_update)) {
mysqli_stmt_execute($stmt_up);
mysqli_stmt_close($stmt_up);
} else {
die("ERROR: " . mysqli_error($link));
}
}