279 lines
7.7 KiB
PHP
279 lines
7.7 KiB
PHP
<?php
|
||
// productpage.php
|
||
|
||
require_once __DIR__ . '/lib/bootstrap.php';
|
||
|
||
// 1) DB-Verbindung (einmal)
|
||
$servername = "localhost";
|
||
$port = 3306;
|
||
$username = "FSST";
|
||
$password = "L9wUNZZ9Qkbt";
|
||
$db = "FSST";
|
||
|
||
$conn = mysqli_connect($servername, $username, $password, $db, $port);
|
||
if (!$conn)
|
||
{
|
||
http_response_code(500);
|
||
die("Datenbankfehler");
|
||
}
|
||
|
||
$productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||
?>
|
||
|
||
<?php include 'header.php'; ?>
|
||
|
||
<?php if ($productId <= 0): ?>
|
||
<section class="product-section">
|
||
<h2>Produkt nicht gefunden</h2>
|
||
<p>Bitte eine gueltige Produkt-ID mitgeben.</p>
|
||
</section>
|
||
|
||
<?php else: ?>
|
||
<?php
|
||
$stmt = $conn->prepare("
|
||
SELECT
|
||
a.name,
|
||
a.unit,
|
||
a.dataType,
|
||
pa.valueString,
|
||
pa.valueNumber,
|
||
pa.valueBool,
|
||
p.model,
|
||
p.description,
|
||
p.imagePath
|
||
FROM products p
|
||
|
||
INNER JOIN categoryAttributes ca
|
||
ON p.categoryID = ca.categoryID
|
||
|
||
INNER JOIN attributes a
|
||
ON ca.attributeID = a.attributeID
|
||
|
||
LEFT JOIN productAttributes pa
|
||
ON pa.productID = p.productID
|
||
AND pa.attributeID = a.attributeID
|
||
|
||
WHERE p.productID = ?
|
||
|
||
ORDER BY a.attributeID
|
||
");
|
||
|
||
|
||
$stmt->bind_param("i", $productId);
|
||
$stmt->execute();
|
||
|
||
$result = $stmt->get_result();
|
||
$product = $result->fetch_assoc();
|
||
|
||
$alreadyInWishlist = false;
|
||
|
||
if (isset($_SESSION['user_id'])) {
|
||
|
||
$stmtCheck = mysqli_prepare(
|
||
$conn,
|
||
"SELECT 1 FROM userFavorites
|
||
WHERE userID = ? AND productID = ?
|
||
LIMIT 1"
|
||
);
|
||
|
||
if ($stmtCheck) {
|
||
mysqli_stmt_bind_param(
|
||
$stmtCheck,
|
||
"ii",
|
||
$_SESSION['user_id'],
|
||
$productId
|
||
);
|
||
|
||
mysqli_stmt_execute($stmtCheck);
|
||
mysqli_stmt_store_result($stmtCheck);
|
||
|
||
if (mysqli_stmt_num_rows($stmtCheck) > 0) {
|
||
$alreadyInWishlist = true;
|
||
}
|
||
|
||
mysqli_stmt_close($stmtCheck);
|
||
}
|
||
}
|
||
?>
|
||
|
||
<?php
|
||
if (
|
||
$_SERVER['REQUEST_METHOD'] === 'POST' &&
|
||
isset($_POST['add_wishlist']) &&
|
||
isset($_SESSION['user_id'])
|
||
) {
|
||
|
||
if (!$alreadyInWishlist) {
|
||
|
||
$stmtFav = mysqli_prepare(
|
||
$conn,
|
||
"INSERT INTO userFavorites (productID, userID) VALUES (?, ?)"
|
||
);
|
||
|
||
if ($stmtFav) {
|
||
mysqli_stmt_bind_param(
|
||
$stmtFav,
|
||
'ii',
|
||
$productId,
|
||
$_SESSION['user_id']
|
||
);
|
||
|
||
mysqli_stmt_execute($stmtFav);
|
||
mysqli_stmt_close($stmtFav);
|
||
|
||
$alreadyInWishlist = true;
|
||
}
|
||
}
|
||
}
|
||
?>
|
||
|
||
<div class="product-wrapper">
|
||
<!-- LINKER BEREICH – BILD -->
|
||
<div class="product-left">
|
||
<div class="product-image-box">
|
||
<img
|
||
src="<?= isset($product['imagePath']) ? $product['imagePath'] : 'assets/images/placeholder.png' ?>"
|
||
alt="<?= htmlspecialchars($product['model'] ?? 'Produktbild') ?>">
|
||
</div>
|
||
<?php if (isset($_SESSION['user_id'])): ?>
|
||
|
||
<?php if ($alreadyInWishlist): ?>
|
||
|
||
<div class="auth__actions">
|
||
<input class="auth__submit" type="button"
|
||
value="Bereits in Wunschliste"
|
||
disabled>
|
||
</div>
|
||
|
||
<?php else: ?>
|
||
|
||
<form method="POST">
|
||
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
||
<input type="hidden" name="add_wishlist" value="1">
|
||
|
||
<div class="auth__actions">
|
||
<input class="auth__submit"
|
||
type="submit"
|
||
value="Zur Wunschliste hinzufügen">
|
||
</div>
|
||
</form>
|
||
|
||
<?php endif; ?>
|
||
|
||
<?php else: ?>
|
||
|
||
<div class="auth__actions">
|
||
<a href="login.php">
|
||
<input class="auth__submit" type="button"
|
||
value="Zum Hinzufügen einloggen">
|
||
</a>
|
||
</div>
|
||
|
||
<?php endif; ?>
|
||
|
||
|
||
</div>
|
||
|
||
<!-- RECHTER BEREICH – DETAILS -->
|
||
<div class="product-right">
|
||
|
||
<h1 class="product-title">
|
||
<?= htmlspecialchars($product['model'] ?? 'Produkt') ?>
|
||
</h1>
|
||
|
||
<div class="product-specs">
|
||
|
||
<div class="product-desc">
|
||
<?= htmlspecialchars($product['description']) ?>
|
||
</div>
|
||
|
||
|
||
<?php
|
||
while ($row = $result->fetch_assoc()) {
|
||
|
||
echo "<p><strong>{$row['name']}:</strong> ";
|
||
|
||
if (!empty($row['valueString'])) echo $row['valueString'];
|
||
if (!empty($row['valueNumber'])) echo $row['valueNumber'] . " " . $row['unit'];
|
||
if (!is_null($row['valueBool'])) echo $row['valueBool'] ? "Ja" : "Nein";
|
||
|
||
echo "</p>";
|
||
}
|
||
?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<?php
|
||
$stmt = mysqli_prepare($conn,
|
||
"SELECT price, shippingCost, inStock, shops.name, offers.offerURL, shops.logoPath, shops.shippingTime
|
||
FROM offers
|
||
INNER JOIN shops ON
|
||
offers.shopID = shops.shopID WHERE offers.productID = ? ORDER BY offers.price ASC");
|
||
|
||
$stmt->bind_param("i", $productId);
|
||
$stmt->execute();
|
||
$result = $stmt->get_result();
|
||
|
||
$shopInfo = [];
|
||
|
||
while ($row = $result->fetch_assoc()) {
|
||
$shopInfo[] = $row;
|
||
}
|
||
|
||
|
||
|
||
?>
|
||
|
||
<div class="shop-offers">
|
||
|
||
<?php if (!empty($shopInfo)): ?>
|
||
|
||
<?php foreach ($shopInfo as $shop): ?>
|
||
|
||
<div class="shop-line">
|
||
|
||
<div class="shop-left">
|
||
<div class="shop-logo">
|
||
<img src="<?= isset($shop['logoPath']) ? $shop['logoPath'] : 'assets/images/placeholder.png' ?>"
|
||
alt ="Kein Logo gefunden" >
|
||
</div>
|
||
<div class="shop-name">
|
||
<a href="<?= htmlspecialchars($shop['offerURL']) ?>" target="_blank">
|
||
<?= htmlspecialchars($shop['name']) ?>
|
||
</a>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="shop-middle">
|
||
<div class="shop-shipping">
|
||
Versand: <?= htmlspecialchars($shop['shippingCost']) ?> €      
|
||
Lieferzeit: <?= htmlspecialchars($shop['shippingTime']) ?> Werktage
|
||
</div>
|
||
<div class="shop-stock <?= $shop['inStock'] ? 'in-stock' : 'out-stock' ?>">
|
||
<?= $shop['inStock'] ? "Lagernd" : "Nicht lagernd" ?>
|
||
</div>
|
||
<div class="shop-price">
|
||
Preis: <?= htmlspecialchars($shop['price']) ?> € <br>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<?php endforeach; ?>
|
||
|
||
<?php else: ?>
|
||
<div class="no-shop">
|
||
<p>Keine Shops bieten dieses Produkt an.</p>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
</div>
|
||
|
||
<?php $stmt->close(); ?>
|
||
|
||
<?php endif; ?>
|
||
|
||
|
||
|
||
|
||
<?php include 'footer.php'; ?>
|