Geizkragen/compare.php

160 lines
8.4 KiB
PHP

<?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((string)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'; ?>