-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.php
182 lines (153 loc) · 4.67 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
session_start();
include 'connection/config.php'; // Database connection
// Handle removing an item from the cart
if (isset($_GET['remove_id'])) {
$remove_id = intval($_GET['remove_id']);
if (($key = array_search($remove_id, $_SESSION['cart'])) !== false) {
unset($_SESSION['cart'][$key]);
$_SESSION['cart'] = array_values($_SESSION['cart']); // Reindex the array
header("Location: cart.php");
exit();
}
}
// Handle clearing the cart
if (isset($_POST['clear_cart'])) {
$_SESSION['cart'] = [];
echo "<script>alert('Cart has been cleared!'); window.location.href='cart.php';</script>";
}
// Fetch product details for cart items
$cart_items = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
$product_details = [];
if (!empty($cart_items)) {
$ids = implode(',', array_map('intval', $cart_items)); // Sanitize input
$sql = "SELECT * FROM books WHERE id IN ($ids)";
$result = $mysqli->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
$product_details[] = $row;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cart</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
}
.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
th {
background-color: #6200ea;
color: #fff;
}
.btn {
background: #6200ea;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
display: inline-block;
margin-top: 10px;
}
.btn:hover {
background: #3700b3;
}
.btn.place-order-btn {
background: #28a745;
}
.btn.place-order-btn:hover {
background: #218838;
}
.btn.remove-btn {
background: #ff0000;
}
.btn.remove-btn:hover {
background: #e60000;
}
.clear-cart-btn {
background: #ff5722;
}
.clear-cart-btn:hover {
background: #e64a19;
}
.total {
font-weight: bold;
text-align: right;
}
</style>
</head>
<body>
<div class="container">
<h1>Your Cart</h1>
<!-- Clear Cart Button -->
<form method="POST" style="display: inline;">
<button type="submit" name="clear_cart" class="btn clear-cart-btn">Clear Cart</button>
</form>
<?php if (empty($product_details)): ?>
<p>Your cart is empty!</p>
<?php else: ?>
<table>
<thead>
<tr>
<th>Book Title</th>
<th>Author</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($product_details as $product): ?>
<tr>
<td><?php echo htmlspecialchars($product['title']); ?></td>
<td><?php echo htmlspecialchars($product['author']); ?></td>
<td>$<?php echo htmlspecialchars($product['price']); ?></td>
<td>
<!-- Remove Button -->
<a href="cart.php?remove_id=<?php echo $product['id']; ?>" class="btn remove-btn">Remove</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="total">
Total: $<?php echo array_sum(array_column($product_details, 'price')); ?>
</div>
<!-- Place Order Button -->
<form action="place_order.php" method="POST">
<button type="submit" class="btn place-order-btn">Place Order</button>
</form>
<a href="products.php" class="btn">Continue Shopping</a>
<?php endif; ?>
<!-- Continue Shopping Button Always Visible -->
<a href="products.php" class="btn" style="margin-top: 20px;">Continue Shopping</a>
</div>
</body>
</html>