-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducts.php
More file actions
293 lines (262 loc) · 12.3 KB
/
products.php
File metadata and controls
293 lines (262 loc) · 12.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
session_start();
include('config.php');
// Initialize the SQL query
$sql = "SELECT * FROM products WHERE 1";
// Add search filter if provided
if (isset($_GET['search']) && !empty($_GET['search'])) {
$search_term = mysqli_real_escape_string($conn, $_GET['search']);
$sql .= " AND name LIKE '%$search_term%'";
}
// Add price range filter if provided
if (isset($_GET['min_price']) && is_numeric($_GET['min_price'])) {
$min_price = (float) $_GET['min_price'];
$sql .= " AND price >= $min_price";
}
if (isset($_GET['max_price']) && is_numeric($_GET['max_price'])) {
$max_price = (float) $_GET['max_price'];
$sql .= " AND price <= $max_price";
}
// Execute the SQL query
$result = mysqli_query($conn, $sql);
// Initialize an array to hold products
$products = [];
if ($result) {
if (mysqli_num_rows($result) > 0) {
while ($product = mysqli_fetch_assoc($result)) {
$products[] = $product;
}
}
} else {
// Handle SQL query error
$error_message = "An error occurred while fetching products. Please try again later.";
}
mysqli_close($conn);
// Get the product id that was added, if any (for alert messages)
$added_id = isset($_GET['added']) ? $_GET['added'] : null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Products - 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;
}
/* Product Card */
.product-card {
border: none;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
/* Product Image */
.product-image {
width: 100%;
height: 300px; /* Fixed height for all images */
object-fit: cover; /* Ensures image covers the entire container */
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
/* Card Title */
.card-title {
font-size: 1.25rem;
font-weight: bold;
color: #333;
}
/* Card Text */
.card-text {
color: #555;
}
/* Add to Cart Button */
.btn-warning {
background-color: #ffc107;
border: none;
color: #343a40;
}
.btn-warning:hover {
background-color: #e0a800;
color: #fff;
}
/* Delete Button */
.delete-btn {
background-color: #dc3545;
border: none;
color: white;
}
.delete-btn:hover {
background-color: #c82333;
}
/* Align text and buttons inside card-body */
.card-body {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.container_mb-5{
display: flex;
margin-left: 200px;
}
</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="container mb-5">
<div class="card">
<div class="card-header bg-warning text-white">
<h5 class="mb-0">Search & Filter Products</h5>
</div>
<div class="card-body">
<form method="GET" class="row g-3">
<!-- Product Name Search -->
<div class="col-md-4">
<label for="search" class="form-label">Product Name</label>
<input type="text" name="search" id="search" class="form-control" placeholder="Enter product name"
value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
</div>
<!-- Minimum Price -->
<div class="col-md-4">
<label for="min_price" class="form-label">Minimum Price</label>
<input type="number" name="min_price" id="min_price" class="form-control" placeholder="Min Price"
value="<?php echo isset($_GET['min_price']) ? htmlspecialchars($_GET['min_price']) : ''; ?>">
</div>
<!-- Maximum Price -->
<div class="col-md-4">
<label for="max_price" class="form-label">Maximum Price</label>
<input type="number" name="max_price" id="max_price" class="form-control" placeholder="Max Price"
value="<?php echo isset($_GET['max_price']) ? htmlspecialchars($_GET['max_price']) : ''; ?>">
</div>
<!-- Submit Button -->
<div class="col-12 text-end">
<button type="submit" class="btn btn-warning">Search</button>
</div>
</form>
</div>
</div>
</div>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<?php if (isset($_SESSION['user_id'])): ?>
<!-- Show Cart link only for non-admin users -->
<?php if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="view_cart.php">Cart</a>
</li>
<li class="nav-item">
<a class="nav-link" href="profile.php">Profile</a>
</li>
<?php endif; ?>
<!-- Admin-only link: Manage Orders -->
<?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="manage_orders.php">Manage Orders</a>
</li>
<li class="nav-item">
<a class="nav-link" href="admin_stats.php">Statistics</a>
</li>
<li class="nav-item">
<a class="nav-link" href="add_product.php">Add Product</a>
</li>
<?php endif; ?>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout (<?php echo htmlspecialchars($_SESSION['name']); ?>)</a>
</li>
<?php else: ?>
<li class="nav-item">
<a class="nav-link" href="register.php">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="login.php">Login</a>
</li>
<?php endif; ?>
</ul>
</div>
</div>
</nav>
<!-- Main Container -->
<div class="container mt-5">
<!-- Display Error Message if any -->
<?php if (isset($error_message)): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($error_message); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<!-- Products Grid -->
<div class="row row-cols-md-3 g-4">
<?php if (!empty($products)): ?>
<?php foreach ($products as $product): ?>
<div class="col">
<div class="card product-card h-100 position-relative">
<?php if ($added_id && $added_id == $product['id']): ?>
<div class="alert alert-success alert-dismissible fade show position-absolute"
style="top: 10px; right: 10px; z-index: 10; min-width: 200px;">
Product added to cart!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<!-- Product Image -->
<img src="<?php echo htmlspecialchars($product['image'] ?: 'https://via.placeholder.com/300x200.png?text=No+Image'); ?>"
class="card-img-top product-image"
alt="<?php echo htmlspecialchars($product['name']); ?>">
<div class="card-body d-flex flex-column">
<!-- Product Name -->
<h5 class="card-title"><?php echo htmlspecialchars($product['name']); ?></h5>
<!-- Product Description -->
<p class="card-text"><?php echo htmlspecialchars($product['description']); ?></p>
<!-- Product Price -->
<p class="card-text"><strong>Price:</strong> $<?php echo number_format($product['price'], 2); ?></p>
<!-- Product Stock -->
<p class="card-text"><strong>Stock:</strong> <?php echo htmlspecialchars($product['stock']); ?></p>
<!-- Only show Add to Cart for non-admin users -->
<?php if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin'): ?>
<form action="add_to_cart.php" method="POST" class="mt-auto">
<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['id']); ?>">
<label for="quantity_<?php echo htmlspecialchars($product['id']); ?>">Quantity:</label>
<input type="number" id="quantity_<?php echo htmlspecialchars($product['id']); ?>"
name="quantity" class="form-control mb-2" value="1"
min="1" max="<?php echo htmlspecialchars($product['stock']); ?>" required>
<button type="submit" class="btn btn-warning w-100">Add to Cart</button>
</form>
<?php endif; ?>
<!-- Admin Controls: Edit and Delete (visible only to admin users) -->
<?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
<div class="mt-3 d-flex justify-content-between">
<!-- Edit Button -->
<a href="edit_product.php?id=<?php echo htmlspecialchars($product['id']); ?>" class="btn btn-primary w-50 me-1">Edit</a>
<!-- Delete Button -->
<form action="delete_product.php" method="POST" class="w-50 ms-1">
<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['id']); ?>">
<button type="submit" class="btn delete-btn w-100">Delete</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="col-12">
<p class="text-center">No products available at the moment. Please check back later.</p>
</div>
<?php endif; ?>
</div>
</div>
<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>