-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart_view.php
More file actions
41 lines (34 loc) · 1.18 KB
/
cart_view.php
File metadata and controls
41 lines (34 loc) · 1.18 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
<?php
session_start();
include "db.php";
include "header.php";
?>
<h2 class="text-center">Shopping Cart</h2>
<?php
if (!isset($_SESSION['cart']) || count($_SESSION['cart']) == 0) {
echo "<p>Your cart is empty.</p>";
} else {
echo '<table class="table">';
echo '<tr><th>Title</th><th>Quantity</th><th>Price</th><th>Total</th><th>Action</th></tr>';
$total_price = 0;
foreach ($_SESSION['cart'] as $book_id => $qty) {
$result = $conn->query("SELECT * FROM books WHERE id = $book_id");
if ($row = $result->fetch_assoc()) {
$total = $row["price"] * $qty;
$total_price += $total;
echo "<tr>
<td>{$row['title']}</td>
<td>$qty</td>
<td>\${$row['price']}</td>
<td>\$$total</td>
<td><a href='cart_remove.php?id=$book_id' class='btn btn-danger btn-sm'>Remove</a></td>
</tr>";
}
}
}
echo "<tr><td colspan='3'><strong>Total</strong></td><td><strong>\$$total_price</strong></td><td></td></tr>";
echo '</table>';
// Checkout Button
echo '<a href="checkout.php" class="btn btn-success">Proceed to Checkout</a>';
?>
<?php include "footer.php"; ?>