Add product comparison feature with dynamic attribute display
This commit is contained in:
parent
6f1bc73073
commit
8ec5f4b3d6
159
compare.php
Normal file
159
compare.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/lib/bootstrap.php';
|
||||
$conn = db_connect();
|
||||
|
||||
$title = "Produktvergleich | Geizkragen";
|
||||
include 'header.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_compare'])) {
|
||||
$removeId = (int)$_POST['remove_compare_id'];
|
||||
if (isset($_SESSION['compare']) && is_array($_SESSION['compare'])) {
|
||||
foreach ($_SESSION['compare'] as $cat => &$prodList) {
|
||||
$key = array_search($removeId, $prodList);
|
||||
if ($key !== false) {
|
||||
unset($prodList[$key]);
|
||||
}
|
||||
}
|
||||
unset($prodList);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="container" style="color: white; margin-top: 2rem;">
|
||||
<h1>Produktvergleich</h1>
|
||||
|
||||
<?php
|
||||
$hasProducts = false;
|
||||
if (isset($_SESSION['compare']) && is_array($_SESSION['compare'])) {
|
||||
foreach ($_SESSION['compare'] as $categoryId => $productIds) {
|
||||
if (empty($productIds)) continue;
|
||||
$hasProducts = true;
|
||||
|
||||
// Kategorie-Namen holen
|
||||
$catName = "Kategorie $categoryId";
|
||||
$stmtCat = $conn->prepare("SELECT name FROM categories WHERE categoryID = ?");
|
||||
if ($stmtCat) {
|
||||
$stmtCat->bind_param("i", $categoryId);
|
||||
$stmtCat->execute();
|
||||
$resCat = $stmtCat->get_result();
|
||||
if ($row = $resCat->fetch_assoc()) {
|
||||
$catName = $row['name'];
|
||||
}
|
||||
$stmtCat->close();
|
||||
}
|
||||
|
||||
// Attribute der Kategorie holen
|
||||
$attributes = [];
|
||||
$stmtAttr = $conn->prepare("
|
||||
SELECT a.attributeID, a.name, a.unit, a.dataType
|
||||
FROM categoryAttributes ca
|
||||
JOIN attributes a ON ca.attributeID = a.attributeID
|
||||
WHERE ca.categoryID = ?
|
||||
ORDER BY a.attributeID
|
||||
");
|
||||
if ($stmtAttr) {
|
||||
$stmtAttr->bind_param("i", $categoryId);
|
||||
$stmtAttr->execute();
|
||||
$resAttr = $stmtAttr->get_result();
|
||||
while ($row = $resAttr->fetch_assoc()) {
|
||||
$attributes[$row['attributeID']] = $row;
|
||||
}
|
||||
$stmtAttr->close();
|
||||
}
|
||||
|
||||
// Produktdaten holen
|
||||
$products = [];
|
||||
$idList = implode(',', array_map('intval', $productIds));
|
||||
$stmtProd = $conn->query("SELECT productID, model, imagePath, description FROM products WHERE productID IN ($idList)");
|
||||
if ($stmtProd) {
|
||||
while ($row = $stmtProd->fetch_assoc()) {
|
||||
$products[$row['productID']] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
// Produktattribute holen
|
||||
$productAttrVals = [];
|
||||
$stmtProdAttr = $conn->query("SELECT productID, attributeID, valueString, valueNumber, valueBool FROM productAttributes WHERE productID IN ($idList)");
|
||||
if ($stmtProdAttr) {
|
||||
while ($row = $stmtProdAttr->fetch_assoc()) {
|
||||
$productAttrVals[$row['productID']][$row['attributeID']] = $row;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<h2 style="margin-top: 2rem; border-bottom: 2px solid #334155; padding-bottom: 0.5rem;"><?= htmlspecialchars($catName) ?></h2>
|
||||
|
||||
<div style="overflow-x: auto; margin-top: 1rem;">
|
||||
<table style="width: 100%; border-collapse: collapse; min-width: 600px;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="padding: 10px; border: 1px solid #334155; background: #1e293b; text-align: left; width: 200px;">Eigenschaft</th>
|
||||
<?php foreach ($productIds as $pId): ?>
|
||||
<?php if (!isset($products[$pId])) continue; ?>
|
||||
<th style="padding: 10px; border: 1px solid #334155; background: #1e293b; text-align: center; width: 250px;">
|
||||
<div style="margin-bottom: 10px;">
|
||||
<a href="productpage.php?id=<?= $pId ?>">
|
||||
<img src="<?= htmlspecialchars($products[$pId]['imagePath'] ?? 'assets/images/placeholder.png') ?>" alt="Produktbild" style="max-height: 100px; display: block; margin: 0 auto; border-radius: 4px;">
|
||||
</a>
|
||||
</div>
|
||||
<h3 style="font-size: 1.1rem; margin: 0;"><a href="productpage.php?id=<?= $pId ?>" style="color: #60a5fa; text-decoration: none;"><?= htmlspecialchars($products[$pId]['model']) ?></a></h3>
|
||||
|
||||
<form method="POST" action="compare.php" style="margin-top: 10px;">
|
||||
<input type="hidden" name="remove_compare" value="1">
|
||||
<input type="hidden" name="remove_compare_id" value="<?= $pId ?>">
|
||||
<button type="submit" style="background: #ef4444; border: none; color: white; padding: 5px 10px; border-radius: 4px; cursor: pointer; font-size: 0.8rem;">Entfernen</button>
|
||||
</form>
|
||||
</th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="padding: 10px; border: 1px solid #334155; font-weight: bold; background: #0f172a;">Beschreibung</td>
|
||||
<?php foreach ($productIds as $pId): ?>
|
||||
<?php if (!isset($products[$pId])) continue; ?>
|
||||
<td style="padding: 10px; border: 1px solid #334155; text-align: left; vertical-align: top; background: <?= $pId % 2 == 0 ? '#1e293b' : '#0f172a' ?>;">
|
||||
<?= htmlspecialchars($products[$pId]['description']) ?>
|
||||
</td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($attributes as $attrId => $attr): ?>
|
||||
<tr>
|
||||
<td style="padding: 10px; border: 1px solid #334155; font-weight: bold; background: #0f172a;"><?= htmlspecialchars($attr['name']) ?></td>
|
||||
<?php foreach ($productIds as $pId): ?>
|
||||
<?php if (!isset($products[$pId])) continue; ?>
|
||||
<td style="padding: 10px; border: 1px solid #334155; text-align: center; background: <?= $pId % 2 == 0 ? '#1e293b' : '#0f172a' ?>;">
|
||||
<?php
|
||||
if (isset($productAttrVals[$pId][$attrId])) {
|
||||
$valRow = $productAttrVals[$pId][$attrId];
|
||||
if (!empty($valRow['valueString'])) {
|
||||
echo htmlspecialchars($valRow['valueString']);
|
||||
} elseif (!empty($valRow['valueNumber']) || $valRow['valueNumber'] === '0.00' || $valRow['valueNumber'] === 0) {
|
||||
echo htmlspecialchars(floatval($valRow['valueNumber'])) . " " . htmlspecialchars($attr['unit']);
|
||||
} elseif ($valRow['valueBool'] !== null) {
|
||||
echo $valRow['valueBool'] ? 'Ja' : 'Nein';
|
||||
} else {
|
||||
echo '-';
|
||||
}
|
||||
} else {
|
||||
echo '-';
|
||||
}
|
||||
?>
|
||||
</td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasProducts) {
|
||||
echo "<p style='margin-top: 2rem;'>Du hast noch keine Produkte zum Vergleich hinzugefügt. Gehe auf eine Produktseite, um Produkte hinzuzufügen.</p>";
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
<?php include 'footer.php'; ?>
|
||||
10
header.php
10
header.php
@ -75,6 +75,9 @@
|
||||
<li class="nav__item nav__item--mobile">
|
||||
<a href="wunschliste.php" class="nav__link">Wunschliste</a>
|
||||
</li>
|
||||
<li class="nav__item nav__item--mobile">
|
||||
<a href="compare.php" class="nav__link">Vergleich</a>
|
||||
</li>
|
||||
<li class="nav__item nav__item--mobile">
|
||||
<a href="account.php" class="nav__link">Account</a>
|
||||
</li>
|
||||
@ -92,6 +95,13 @@
|
||||
<!-- ═══ /Slide-In-Menü ═══ -->
|
||||
|
||||
<div class="nav__actions">
|
||||
<a class="nav__login nav__wishlist" href="compare.php" aria-label="Vergleich">
|
||||
<svg class="icon icon-user" viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<line x1="18" y1="20" x2="18" y2="10"></line>
|
||||
<line x1="12" y1="20" x2="12" y2="4"></line>
|
||||
<line x1="6" y1="20" x2="6" y2="14"></line>
|
||||
</svg>
|
||||
</a>
|
||||
<a class="nav__login nav__wishlist" href="wunschliste.php" aria-label="Wunschliste">
|
||||
<svg class="icon icon-user" viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path d="M16 2H8a3 3 0 0 0-3 3v16a1 1 0 0 0 .5.87a1 1 0 0 0 1 0l5.5-3.18l5.5 3.18a1 1 0 0 0 .5.13a1 1 0 0 0 .5-.13A1 1 0 0 0 19 21V5a3 3 0 0 0-3-3Zm1 17.27l-4.5-2.6a1 1 0 0 0-1 0L7 19.27V5a1 1 0 0 1 1-1h8a1 1 0 0 1 1 1Z"
|
||||
|
||||
@ -50,7 +50,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_review']) && i
|
||||
pa.valueBool,
|
||||
p.model,
|
||||
p.description,
|
||||
p.imagePath
|
||||
p.imagePath,
|
||||
p.categoryID
|
||||
FROM products p
|
||||
|
||||
INNER JOIN categoryAttributes ca
|
||||
@ -74,6 +75,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_review']) && i
|
||||
|
||||
$result = $stmt->get_result();
|
||||
$product = $result->fetch_assoc();
|
||||
$categoryId = $product['categoryID'];
|
||||
|
||||
$alreadyInWishlist = false;
|
||||
|
||||
@ -135,6 +137,25 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Vergleichslogik
|
||||
if (!isset($_SESSION['compare'])) {
|
||||
$_SESSION['compare'] = [];
|
||||
}
|
||||
if (!isset($_SESSION['compare'][$categoryId])) {
|
||||
$_SESSION['compare'][$categoryId] = [];
|
||||
}
|
||||
$alreadyInCompare = in_array($productId, $_SESSION['compare'][$categoryId]);
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (isset($_POST['add_compare']) && !$alreadyInCompare) {
|
||||
$_SESSION['compare'][$categoryId][] = $productId;
|
||||
$alreadyInCompare = true;
|
||||
} elseif (isset($_POST['remove_compare']) && $alreadyInCompare) {
|
||||
$_SESSION['compare'][$categoryId] = array_diff($_SESSION['compare'][$categoryId], [$productId]);
|
||||
$alreadyInCompare = false;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
@ -201,6 +222,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($alreadyInCompare): ?>
|
||||
<form method="POST">
|
||||
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
||||
<input type="hidden" name="remove_compare" value="1">
|
||||
<div class="auth__actions" style="margin-top: 10px;">
|
||||
<input class="auth__submit" type="submit" value="Aus Vergleich entfernen" style="background: #eab308; border-color: #eab308; color: white;">
|
||||
</div>
|
||||
</form>
|
||||
<?php else: ?>
|
||||
<form method="POST">
|
||||
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
||||
<input type="hidden" name="add_compare" value="1">
|
||||
<div class="auth__actions" style="margin-top: 10px;">
|
||||
<input class="auth__submit" type="submit" value="Zum Vergleich hinzufügen" style="background: #3b82f6; border-color: #3b82f6; color: white;">
|
||||
</div>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="review-overview-box">
|
||||
<?php if ($reviewOverview['reviewCount'] > 0): ?>
|
||||
<div class="overview-header">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user