feat: add search functionality with results display and remove autocomplete script

This commit is contained in:
Fabian Schieder 2026-03-18 16:06:04 +01:00
parent a250189b08
commit c1cb39e083
3 changed files with 59 additions and 4 deletions

View File

@ -11,6 +11,64 @@ error_reporting(E_ALL);
$conn = db_connect();
?>
<?php
// ─────────────────────────────────────────────
// Reine PHP-Suche (GET ?search=...)
// Wenn ein Suchbegriff vorhanden ist, zeigen wir nur Suchergebnisse
// (statt der Kategorie-Sektionen).
// ─────────────────────────────────────────────
$searchTerm = isset($_GET['search']) ? trim((string)$_GET['search']) : '';
if ($searchTerm !== '' && mb_strlen($searchTerm, 'UTF-8') >= 2) {
$like = addcslashes($searchTerm, "%_\\");
$like = '%' . $like . '%';
$stmtSearch = $conn->prepare("
SELECT productID, model, description, imagePath
FROM products
WHERE model LIKE ? OR description LIKE ?
ORDER BY model ASC
LIMIT 60
");
if ($stmtSearch) {
$stmtSearch->bind_param('ss', $like, $like);
$stmtSearch->execute();
$resultSearch = $stmtSearch->get_result();
?>
<section class="product-section">
<h2>Suchergebnisse für <?= htmlspecialchars($searchTerm) ?>“</h2>
<?php if ($resultSearch->num_rows <= 0): ?>
<p style="color: var(--text-secondary); margin-top: 8px;">Keine Produkte gefunden.</p>
<?php else: ?>
<div class="product-grid" style="margin-top: 14px;">
<?php while ($product = $resultSearch->fetch_assoc()): ?>
<?php $productId = (int)$product['productID']; ?>
<a class="product-card" href="productpage.php?id=<?= $productId ?>">
<img
src="<?= !empty($product['imagePath']) ? htmlspecialchars($product['imagePath']) : 'assets/images/placeholder.png' ?>"
alt="<?= htmlspecialchars($product['model'] ?? '') ?>">
<div class="product-card__content">
<h3><?= htmlspecialchars($product['model'] ?? '') ?></h3>
<p><?= htmlspecialchars($product['description'] ?? '') ?></p>
</div>
</a>
<?php endwhile; ?>
</div>
<?php endif; ?>
</section>
<?php
$stmtSearch->close();
}
// Wichtig: In Suchmodus KEINE Kategorien rendern.
return;
}
?>
<?php
$activeCategory = isset($_GET['category']) ? $_GET['category'] : 'all';
?>

View File

@ -162,6 +162,3 @@
})();
</script>
<!-- Produktsuche (Autocomplete Dropdown) -->
<script src="assets/js/search-autocomplete.js" defer></script>

View File

@ -239,7 +239,7 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<?php
$stmt = mysqli_prepare($conn,
"SELECT price, shippingCost, inStock, shops.name, offers.offerURL, shops.logoPath, shops.shippingTime
"SELECT price, shippingCost, inStock, shops.name, offers.productURL, shops.logoPath, shops.shippingTime
FROM offers
INNER JOIN shops ON
offers.shopID = shops.shopID WHERE offers.productID = ? ORDER BY offers.price ASC");