-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewCart.php
More file actions
75 lines (56 loc) · 2.74 KB
/
Copy pathViewCart.php
File metadata and controls
75 lines (56 loc) · 2.74 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
<?php // <--- do NOT put anything before this PHP tag
include('./functions.php');
include('./views/partials/head.php');
include('./views/partials/navbar.php');
// get the cookieMessage, this must be done before any HTML is sent to the browser.
// does the user have items in the shopping cart?
if (isset($_COOKIE['ShoppingCart']) && $_COOKIE['ShoppingCart'] != '') {
// <===*** Quantity is passed along with id in cookie separated by '=' ***===>
$map = [];
foreach (explode(',', $_COOKIE['ShoppingCart']) as $pair) {
[$id, $tag] = explode('=', $pair);
$map[$id] = $tag; // This already removes any duplicates so no need for array_unique calls;
}
$ids = array_keys($map);
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$dbh = connectToDatabase();
// create a SQL statement to select the product and brand info about a given ProductID
// this SQL statement will be very similar to the one in ViewProduct.php
$statement = $dbh->prepare("
SELECT * FROM Products AS P INNER JOIN Brands AS B ON P.BrandID = B.BrandID WHERE P.ProductID IN ($placeholders) ;
");
$statement->execute($ids);
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
// <===*** Using the famous spread operator (I borrowed it from my JS knowledge) ***===>
// OUR SERVER DOESN'T SUPPORT THIS SPREAD OPERATOR....APPARENTLY THE VERSION IS OLDER THAN 7.4
// $rows = array_map(fn($row) => [...$row, 'Quantity' => $map[$row['ProductID']], 'SubTotal' => $map[$row['ProductID']] * $row['Price']], $rows);
$newRows = [];
foreach ($rows as $row) {
// Make a copy of the row
$row['Quantity'] = $map[$row['ProductID']] ?? 0;
$row['SubTotal'] = $row['Quantity'] * $row['Price'];
$newRows[] = $row; // push the modified row
}
// Replace original rows array
$rows = $newRows;
// break reference
// TODO: output the $totalPrice.
$totalPrice = array_sum(array_column($rows, 'SubTotal'));
// here array_column extracts values of a column (or key for associative array)
// then array_sum performs mathematical sum
$heading = 'Your Cart has ' . count($rows) . " items";
$bannerMessage = 'Thank you for shopping with us. See you again soon!';
include('./views/partials/banner.php');
include './views/ViewCart.view.php';
// you are allowed to stop and start the PHP tags so you don't need to use lots of echo statements.
} else {
$heading = 'Your Cart Is Empty';
include('./views/partials/banner.php');
setCookieMessage("Checkout today's hot deals to get the best offers.");
echo "
<main class='ViewCart-main'>
<div class='product-empty'>
<p>Browse our <a class=\"Browse-products-link\" href=\"./ProductList.php\">products</a> and add items to your cart!</p>
</div>
</main>";
}