-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
88 lines (74 loc) · 2.88 KB
/
cart.php
File metadata and controls
88 lines (74 loc) · 2.88 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
<?php
// Starts the user session
session_start();
// Header text for this page
$headerText = "<h1 class=\"display-1\">My Cart</h1>";
// If the input "add to cart" is set (clicked), sets the $_SESSION to the $_POST ["cart"]
if (isset($_POST["cart"])) {
$orderedItem = [];
if (isset($_POST["quantity"])) {
$orderedItem["quantity"] = $_POST["quantity"];
}
if (isset($_POST["name"])){
$orderedItem["name"] = $_POST["name"];
}
if (isset($_POST["price"])){
$orderedItem["price"] = $_POST["price"];
}
if (isset($_POST["id"])){
$orderedItem["id"] = $_POST["id"];
}
if (!isset($_SESSION["order"])) {
$_SESSION["order"] = [];
}
$_SESSION["order"][]= $orderedItem;
}
// Unsets the session data aka, clears the cart
if (isset($_POST["deleteSession"])) {
unset($_SESSION ["order"]);
}
//Deletes individual items from the session
unset($_SESSION ["order"][$_GET["index"]]);
?>
<!DOCTYPE html>
<html>
<!-- Gets the header -->
<?php include("header.php"); ?>
<!-- Main content begins -->
<div class="container">
<form class="cart" action="cart.php" method="post">
<h3>Cart</h3>
<div class="row cart-heading">
<div class="col-md-2">Quantity</div>
<div class="col-md-5">Name</div>
<div class="col-md-1">Price</div>
<div class="col-md-1">Subtotal</div>
<div class="col-md-1"></div>
</div>
<!-- Retrieves data in a loop -->
<?php
//Iterates over each session array variable to get the quantities, names, and prices of the products
foreach ($_SESSION["order"] as $index =>$item){ ?>
<div class="row">
<div class="col-md-2"> <?php echo ($item["quantity"]); ?></div>
<div class="col-md-5"> <?php echo ($item ["name"]); ?></div>
<div class="col-md-1">$<?php echo ($item ["price"]); ?> </div>
<?php
$miniTotal = ($item ["price"])*($item["quantity"]);
$cartTotal += $miniTotal;
?>
<div class="col-md-1">$<?php echo ($miniTotal); ?></div>
<div class="col-md-1"><a href="cart.php?index=<?php echo ($index); ?>">Remove</a> </div>
</div>
<?php } ?>
<hr>
<!-- Displays Grand Total -->
<h4 class="order-total"> Grand Total: $<?php echo $cartTotal ?> </h4>
<a href="index.php"><button type="button" name="button" class="button"> Keep Shoppin'</button></a>
<a href="order.php" class="right"><button type="button" name="button" class="button"> Proceed to Checkout</button></a>
</form>
</div>
<!-- Gets the footer -->
<?php include("footer.php") ?>
</body>
</html>