<?php
// --- Load only Spices & Herbs products ---
$stmt = $conn->prepare("SELECT * FROM products WHERE category = ?");
$category = 'spices';
$stmt->bind_param("s", $category);
$stmt->execute();
$result = $stmt->get_result();
if ($result && $result->num_rows > 0):
while ($row = $result->fetch_assoc()):
// --- Default / fallback image ---
$imageSrc = 'Admin/uploads/products/no-image.png';
// --- Clean and normalize image path ---
$rawImage = trim($row['image'] ?? '');
if (!empty($rawImage)) {
// Handle JSON-like stored paths (e.g. ["uploads/products/img.png"])
$rawImage = trim($rawImage, "[]\"'");
// Case 1: Full URL (e.g. https://example.com/uploads/products/img.png)
if (preg_match('/^https?:\/\//i', $rawImage)) {
$imageSrc = $rawImage;
}
// Case 2: Starts with "Admin/uploads/products/"
elseif (strpos($rawImage, 'Admin/uploads/products/') === 0) {
$imageSrc = $rawImage;
}
// Case 3: Starts with "uploads/products/"
elseif (strpos($rawImage, 'uploads/products/') === 0) {
$imageSrc = 'Admin/' . $rawImage;
}
// Case 4: Only filename (e.g. "img.png")
else {
$imageSrc = 'Admin/uploads/products/' . $rawImage;
}
}
// --- Shorten description (limit 120 chars) ---
$description = strip_tags($row['description'] ?? '');
$shortDescription = mb_strimwidth($description, 0, 120, '...');
?>
<!-- Example display block -->
<div class="col-lg-3 col-md-4 col-sm-6 mb-4">
<div class="card product-card h-100 shadow-sm">
<img src="<?= htmlspecialchars($imageSrc) ?>" class="card-img-top" alt="<?= htmlspecialchars($row['name']) ?>">
<div class="card-body">
<h5 class="card-title"><?= htmlspecialchars($row['name']) ?></h5>
<p class="card-text small text-muted"><?= htmlspecialchars($shortDescription) ?></p>
<a href="Product.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-outline-danger w-100">View Product</a>
</div>
</div>
</div>
<?php
endwhile;
else:
?>
<p class="text-center text-muted mt-5">No spices available at the moment.</p>
<?php endif; ?>