-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
117 lines (105 loc) · 4.08 KB
/
Copy pathfunctions.php
File metadata and controls
117 lines (105 loc) · 4.08 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
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
<?php
// Function to validate numbers
function pf_validate_number($value, $function, $redirect) {
if(isset($value)) {
if(!is_numeric($value)) {
header("Location: " . $redirect);
exit;
}
return (int)$value;
}
else {
if($function == 'redirect') {
header("Location: " . $redirect);
exit;
}
return 0;
}
}
// Function to show cart
function showcart() {
global $config_basedir, $db;
$total = 0;
// Check if order exists
if(isset($_SESSION['SESS_ORDERNUM'])) {
if(isset($_SESSION['SESS_LOGGEDIN'])) {
// For logged in users
$custsql = "SELECT id, status FROM orders WHERE customer_id = ? AND status < 2";
$stmt = mysqli_prepare($db, $custsql);
mysqli_stmt_bind_param($stmt, "i", $_SESSION['SESS_USERID']);
mysqli_stmt_execute($stmt);
$custres = mysqli_stmt_get_result($stmt);
$custrow = mysqli_fetch_assoc($custres);
mysqli_stmt_close($stmt);
}
else {
// For guests
$custsql = "SELECT id, status FROM orders WHERE session = ? AND status < 2";
$stmt = mysqli_prepare($db, $custsql);
$sess_id = session_id();
mysqli_stmt_bind_param($stmt, "s", $sess_id);
mysqli_stmt_execute($stmt);
$custres = mysqli_stmt_get_result($stmt);
$custrow = mysqli_fetch_assoc($custres);
mysqli_stmt_close($stmt);
}
if($custrow) {
$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS itemid
FROM products, orderitems
WHERE orderitems.product_id = products.id AND order_id = ?";
$stmt = mysqli_prepare($db, $itemssql);
mysqli_stmt_bind_param($stmt, "i", $custrow['id']);
mysqli_stmt_execute($stmt);
$itemsres = mysqli_stmt_get_result($stmt);
$itemnumrows = mysqli_num_rows($itemsres);
}
else {
$itemnumrows = 0;
}
}
else {
$itemnumrows = 0;
}
if($itemnumrows == 0) {
echo "<p>You have not added anything to your shopping cart yet.</p>";
return;
}
// Display cart contents
echo "<table cellpadding='10'>";
echo "<tr>";
echo "<td></td>";
echo "<td><strong>Item</strong></td>";
echo "<td><strong>Quantity</strong></td>";
echo "<td><strong>Unit Price</strong></td>";
echo "<td><strong>Total Price</strong></td>";
echo "<td></td>";
echo "</tr>";
while($itemsrow = mysqli_fetch_assoc($itemsres)) {
$quantitytotal = $itemsrow['price'] * $itemsrow['quantity'];
echo "<tr>";
if(empty($itemsrow['image'])) {
echo "<td><img src='./images/No Image.jpg' width='50' alt='" . htmlspecialchars($itemsrow['name']) . "'></td>";
}
else {
echo "<td><img src='./images/" . htmlspecialchars($itemsrow['image']) . "' width='50' alt='" . htmlspecialchars($itemsrow['name']) . "'></td>";
}
echo "<td>" . htmlspecialchars($itemsrow['name']) . "</td>";
echo "<td>" . htmlspecialchars($itemsrow['quantity']) . "</td>";
echo "<td><strong>£" . sprintf('%.2f', $itemsrow['price']) . "</strong></td>";
echo "<td><strong>£" . sprintf('%.2f', $quantitytotal) . "</strong></td>";
echo "<td>[<a href='" . htmlspecialchars($config_basedir) . "delete.php?id=" . htmlspecialchars($itemsrow['itemid']) . "'>REMOVE</a>]</td>";
echo "</tr>";
$total += $quantitytotal;
}
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>TOTAL</td>";
echo "<td><strong>£" . sprintf('%.2f', $total) . "</strong></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
mysqli_stmt_close($stmt);
}
?>