-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
95 lines (85 loc) · 3.29 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
<?php
session_start();
require_once "./functions/database_functions.php";
require_once "./functions/cart_functions.php";
// book_isbn got from form post method, change this place later.
if (isset($_POST['bookisbn'])) {
$book_isbn = $_POST['bookisbn'];
}
if (isset($book_isbn)) {
// new iem selected
if (!isset($_SESSION['cart'])) {
// $_SESSION['cart'] is associative array that bookisbn => qty
$_SESSION['cart'] = [];
$_SESSION['total_items'] = 0;
$_SESSION['total_price'] = '0.00';
}
if (!isset($_SESSION['cart'][$book_isbn])) {
$_SESSION['cart'][$book_isbn] = 1;
} elseif (isset($_POST['cart'])) {
$_SESSION['cart'][$book_isbn]++;
unset($_POST);
}
}
// if save change button is clicked , change the qty of each bookisbn
if (isset($_POST['save_change'])) {
foreach ($_SESSION['cart'] as $isbn => $qty) {
if ($_POST[$isbn] == '0') {
unset($_SESSION['cart']["$isbn"]);
} else {
$_SESSION['cart']["$isbn"] = $_POST["$isbn"];
}
}
}
$title = "Your shopping cart";
require "./template/header.php";
if (isset($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) {
$_SESSION['total_price'] = total_price($_SESSION['cart']);
$_SESSION['total_items'] = total_items($_SESSION['cart']);
?>
<form action="cart.php" method="post">
<table class="table table-bg">
<tr class="table-heading-bg">
<th class="big-font-1">Book Title</th>
<th class="big-font-1">Price</th>
<th class="big-font-1">Quantity</th>
<th class="big-font-1">Total</th>
</tr>
<?php
$conn = db_connect();
foreach ($_SESSION['cart'] as $isbn => $qty) {
$book = mysqli_fetch_assoc(getBookByIsbn($conn, $isbn));
?>
<tr>
<td><?php echo $book['book_title'] . " by " . $book['book_author']; ?></td>
<td><?php echo "$" . $book['book_price']; ?></td>
<td><input type="number" value="<?php echo abs($qty); ?>" size="5" name="<?php echo $isbn; ?>"></td>
<td><?php echo "$" . $qty * $book['book_price']; ?></td>
</tr>
<?php } ?>
<tr>
<th> </th>
<th> </th>
<th><?php echo $_SESSION['total_items']; ?></th>
<th><?php echo "$" . $_SESSION['total_price']; ?></th>
</tr>
</table>
<input type="submit" class="btn btn-primary" name="save_change" value="Save Changes">
</form>
<br/><br/>
<div class="container text-center">
<!-- created empty cart button, if user clicks it, cart will be emptied, sessions will be destroyed and redirected to cart -->
<a href="empty_session.php" class="btn btn-primary">Empty Cart</a>
<a href="checkout.php" class="btn btn-primary">Go To Checkout</a>
<a href="books.php" class="btn btn-primary">Continue Shopping</a>
</div>
<?php
} else {
$_SESSION["errorArray"]["emtpyCart"] = "Your Cart is Empty. Make Sure You Add Some Books in it";
header("location:nothingFound.php");
}
if (isset($conn)) {
mysqli_close($conn);
}
require_once "./template/footer.php";
?>