36766-vm/products.php
Flatlogic Bot 6c14b2436f 2.0
2025-12-18 09:40:37 +00:00

123 lines
5.3 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
try {
$pdo = db();
// Fetch categories for filter dropdown
$category_stmt = $pdo->query("SELECT * FROM product_categories ORDER BY name ASC");
$categories = $category_stmt->fetchAll();
// Get filter parameters
$search = $_GET['search'] ?? '';
$selected_category = $_GET['category'] ?? '';
// Build product query
$sql = "SELECT p.*, c.name AS category_name FROM products p LEFT JOIN product_categories c ON p.category_id = c.id";
$params = [];
$where_clauses = [];
if ($search) {
$where_clauses[] = "(p.name LIKE ? OR p.model_number LIKE ?)";
$params[] = '%' . $search . '%';
$params[] = '%' . $search . '%';
}
if ($selected_category) {
$where_clauses[] = "p.category_id = ?";
$params[] = $selected_category;
}
if (!empty($where_clauses)) {
$sql .= " WHERE " . implode(" AND ", $where_clauses);
}
$sql .= " ORDER BY p.name ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$products = $stmt->fetchAll();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<h1 class="mb-4">Product Catalog</h1>
<form method="GET" action="products.php" class="mb-4">
<div class="row g-3">
<div class="col-md-6">
<input type="text" class="form-control" placeholder="Search by product name or model..." name="search" value="<?php echo htmlspecialchars($search); ?>">
</div>
<div class="col-md-4">
<select name="category" class="form-select">
<option value="">All Categories</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category['id']; ?>" <?php if ($selected_category == $category['id']) echo 'selected'; ?>>
<?php echo htmlspecialchars($category['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<button class="btn btn-primary w-100" type="submit">Filter</button>
</div>
</div>
</form>
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
<?php if (empty($products)): ?>
<div class="col">
<p>No products found.</p>
</div>
<?php else: ?>
<?php foreach ($products as $product): ?>
<div class="col">
<div class="card h-100">
<img src="<?php echo htmlspecialchars($product['image_url']); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($product['name']); ?>">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($product['name']); ?></h5>
<p class="card-text"><strong>Price:</strong> $<?php echo htmlspecialchars(number_format($product['price'], 2)); ?></p>
<h6 class="card-subtitle mb-2 text-muted">Model: <?php echo htmlspecialchars($product['model_number']); ?></h6>
<?php if ($product['category_name']): ?>
<h6 class="card-subtitle mb-2 text-muted">Category: <?php echo htmlspecialchars($product['category_name']); ?></h6>
<?php endif; ?>
<p class="card-text"><?php echo htmlspecialchars($product['description']); ?></p>
<?php if ($product['features']): ?>
<p class="card-text"><small><strong>Features:</strong> <?php echo htmlspecialchars($product['features']); ?></small></p>
<?php endif; ?>
<?php if ($product['sample_type']): ?>
<p class="card-text"><small><strong>Sample Type:</strong> <?php echo htmlspecialchars($product['sample_type']); ?></small></p>
<?php endif; ?>
<?php if ($product['measurement_parameters']): ?>
<p class="card-text"><small><strong>Parameters:</strong> <?php echo htmlspecialchars($product['measurement_parameters']); ?></small></p>
<?php endif; ?>
<?php if ($product['result_speed']): ?>
<p class="card-text"><small><strong>Result Speed:</strong> <?php echo htmlspecialchars($product['result_speed']); ?></small></p>
<?php endif; ?>
<div class="mt-auto">
<form action="cart.php?action=add" method="post">
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
<div class="input-group mb-3">
<input type="number" name="quantity" id="quantity-<?php echo $product['id']; ?>" class="form-control" value="1" min="1">
<button type="submit" class="btn btn-primary">Add to Cart</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php require_once 'includes/footer.php'; ?>