Geizkragen/compcards.php

143 lines
4.9 KiB
PHP

<?php
// login.php
require_once __DIR__ . '/lib/bootstrap.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// 1) DB-Verbindung (einmal)
$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']) : '';
$searchLen = function_exists('mb_strlen') ? mb_strlen($searchTerm, 'UTF-8') : strlen($searchTerm);
if ($searchTerm !== '') {
if ($searchLen < 2) {
?><section class="product-section">
<h2>Suchergebnisse für „<?= htmlspecialchars($searchTerm) ?>“</h2>
<p class="search-empty">Bitte geben Sie mindestens 2 Zeichen ein.</p>
</section><?php
return;
}
$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 class="search-empty">Keine Produkte gefunden.</p>
<?php else: ?>
<div class="product-grid">
<?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';
?>
<?php
$categories = [
'iphone' => ['id' => 20, 'label' => 'iPhone'],
'ipad' => ['id' => 21, 'label' => 'iPad'],
'macbook' => ['id' => 22, 'label' => 'MacBook'],
'airpods' => ['id' => 23, 'label' => 'AirPods'],
'accessories' => ['id' => 24, 'label' => 'Accessories'],
];
?>
<?php foreach ($categories as $key => $cat): ?>
<?php if ($activeCategory === 'all' || $activeCategory === $key): ?>
<?php
$stmt = $conn->prepare("
SELECT productID, model, description, imagePath
FROM products
WHERE categoryID = ?
");
$stmt->bind_param("i", $cat['id']); // i = integer
$stmt->execute();
$result = $stmt->get_result();
?>
<?php if ($result->num_rows > 0): ?>
<section class="product-section">
<h2><?= htmlspecialchars($cat['label']) ?></h2>
<div class="product-scroll">
<?php while ($product = $result->fetch_assoc()): ?>
<?php $productId = (int)$product['productID']; ?>
<a class="product-card" href="productpage.php?id=<?= $productId ?>">
<img
src="<?= isset($product['imagePath']) ? $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>
</section>
<?php endif; ?>
<?php $stmt->close(); ?>
<?php endif; ?>
<?php endforeach; ?>