-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_cart.php
More file actions
128 lines (121 loc) · 5.3 KB
/
view_cart.php
File metadata and controls
128 lines (121 loc) · 5.3 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
118
119
120
121
122
123
124
125
126
127
128
<?php
session_start();
include('config.php');
// Ensure the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
// Fetch the cart items along with product details
$sql = "SELECT c.product_id, c.quantity, p.name, p.description, p.price, p.image
FROM cart c
LEFT JOIN products p ON c.product_id = p.id
WHERE c.user_id='$user_id'";
$result = mysqli_query($conn, $sql);
$cart_items = [];
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$cart_items[] = $row;
}
} else {
$error_message = "An error occurred while fetching your cart. Please try again later.";
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Cart - Honey E-Commerce</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; }
.table th, .table td { vertical-align: middle; }
</style>
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="index.php">Honey E-Commerce</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="products.php">Products</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout (<?php echo htmlspecialchars($_SESSION['name']); ?>)</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Main Container -->
<div class="container mt-5">
<h2 class="mb-4">Your Shopping Cart</h2>
<?php if(isset($error_message)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<?php if(empty($cart_items)): ?>
<p>Your cart is empty. <a href="products.php">Go shopping!</a></p>
<?php else: ?>
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $total = 0; ?>
<?php foreach($cart_items as $item): ?>
<?php $subtotal = $item['price'] * $item['quantity']; ?>
<?php $total += $subtotal; ?>
<tr>
<td>
<?php if(!empty($item['image'])): ?>
<img src="<?php echo htmlspecialchars($item['image']); ?>" alt="<?php echo htmlspecialchars($item['name']); ?>" style="width:80px; height:80px; object-fit:cover;">
<?php else: ?>
<img src="https://via.placeholder.com/80" alt="No Image">
<?php endif; ?>
<br>
<?php echo htmlspecialchars($item['name']); ?>
</td>
<td><?php echo htmlspecialchars($item['description']); ?></td>
<td>$<?php echo number_format($item['price'], 2); ?></td>
<td><?php echo htmlspecialchars($item['quantity']); ?></td>
<td>$<?php echo number_format($subtotal, 2); ?></td>
<td>
<!-- Remove item from cart -->
<form action="remove_from_cart.php" method="POST">
<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($item['product_id']); ?>">
<button type="submit" class="btn btn-danger btn-sm">Remove</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="4" class="text-end"><strong>Total:</strong></td>
<td colspan="2"><strong>$<?php echo number_format($total, 2); ?></strong></td>
</tr>
</tbody>
</table>
<div class="text-end">
<a href="checkout.php" class="btn btn-success">Proceed to Checkout</a>
</div>
<?php endif; ?>
</div>
<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>