Загрузка данных


<?php
session_start();

// Пример массива товаров (замените на запрос к БД в реальном проекте)
$products = [
    1 => [
        'name' => 'Монитор AOC 24"',
        'image' => 'https://via.placeholder.com/600x400?text=Монитор+AOC+24"',
        'description' => 'Отличный монитор для работы и игр с диагональю 24 дюйма и высоким разрешением.',
        'characteristics' => [
            'Диагональ' => '24"',
            'Разрешение' => '1920x1080 (Full HD)',
            'Матрица' => 'IPS',
            'Частота обновления' => '75 Гц',
            'Интерфейсы' => 'HDMI, DisplayPort, VGA'
        ],
        'price' => 15990
    ],
    2 => [
        'name' => 'Мышь Razer DeathAdder',
        'image' => 'https://via.placeholder.com/600x400?text=Razer+DeathAdder',
        'description' => 'Игровая мышь с высокой точностью сенсора и эргономичным дизайном.',
        'characteristics' => [
            'Тип подключения' => 'Проводная',
            'Сенсор' => 'Оптический',
            'DPI' => 'До 6400',
            'Количество кнопок' => '5'
        ],
        'price' => 4990
    ],
    // ... другие товары
];

// Получаем ID товара из URL
$product_id = $_GET['id'] ?? null;

// Проверяем, существует ли товар
if (!isset( $products[$product_id])) {
    echo '<h1>Товар не найден</h1>';
    exit;
}

$product = $products[$product_id];
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars( $product['name']) ?></title>
    <style>
        * { margin:0; padding:0; box-sizing:border-box; }
        body { font-family:Arial, sans-serif; background:#f4f4f4; color:#333; }
        .container { max-width:1200px; margin:auto; padding:20px; }
        .breadcrumbs { margin-bottom:20px; }
        .breadcrumbs a { color:#27ae60; text-decoration:none; }
        .product-page { display:flex; flex-wrap:wrap; gap:30px; background:#fff; padding:25px; border-radius:8px; box-shadow:0 2px 8px rgba(0,0,0,.1); }
        .product-image img { width:100%; max-width:450px; height:auto; border-radius:6px; }
        .product-details { flex:1; min-width:300px; }
        .product-name { font-size:1.8em; font-weight:bold; margin-bottom:15px; }
        .product-price { color:#e74c3c; font-size:1.5em; font-weight:bold; margin-bottom:25px; }
        .product-description { margin-bottom:25px; line-height:1.6; }
        .characteristics-table { width:100%; border-collapse:collapse; margin-bottom:25px; }
        .characteristics-table th, .characteristics-table td { padding:8px 0; border-bottom:1px solid #ddd; text-align:left; }
        .characteristics-table th { font-weight:bold; color:#555; }
        .btn-add-to-cart {
            display:inline-block;
            background:#27ae60;
            color:#fff;
            padding:12px 25px;
            text-decoration:none;
            border-radius:6px;
            font-weight:bold;
            transition:background .3s;
            margin-top:-38px;
         }
         .btn-add-to-cart:focus, .btn-add-to-cart:focus-visible, .btn-add-to-cart:focus-within { outline:none; box-shadow:none; }
         .btn-add-to-cart:hover { background:#2ecc71; }
         .cart-link {
             position:fixed;
             top:20px;
             right:20px;
             background:#e74c3c;
             color:#fff;
             padding:10px 15px;
             text-decoration:none;
             border-radius:4px;
             z-index:100;
         }
    </style>
</head>
<body>

<div class="container">

    <!-- Хлебные крошки -->
    <div class="breadcrumbs">
        <a href="index.php">Главная</a> > 
        <a href="products.php?category_id=<?= $product['category_id'] ?? 1 ?>">Категория</a> > 
        <?= htmlspecialchars( $product['name']) ?>
    </div>

    <h1><?= htmlspecialchars( $product['name']) ?></h1>

    <!-- Блок товара -->
    <div class="product-page">

      <!-- Изображение -->
      <div class="product-image">
          <img alt="<?= htmlspecialchars( $product['name']) ?>" alt="<?= htmlspecialchars( $product['name']) ?>">
      </div>

      <!-- Описание и цена -->
      <div class="product-details">
          <div class="product-price"><?= number_format( $product['price'], 0, '', ' ') ?> ₽</div>
          <div class="product-description"><?= nl2br(htmlspecialchars( $product['description'])) ?></div>

          <!-- Характеристики -->
          <h3>Характеристики</h3>
          <table class="characteristics-table">
              <?php foreach ( $product['characteristics'] as $key => $value): ?>
                  <tr>
                      <th><?= htmlspecialchars( $key) ?>:</th>
                      <td><?= htmlspecialchars( $value) ?></td>
                  </tr>
              <?php endforeach; ?>
          </table>

          <!-- Кнопка "Добавить в корзину" -->
          <?php if (isset( $_SESSION['user'])) : ?>
              <a href="?add_to_cart=<?= $product_id ?>" class="btn-add-to-cart">Добавить в корзину</a>
          <?php else : ?>
              <p>Чтобы добавить товар в корзину, <a href="login.php">войдите в аккаунт</a>.</p>
          <?php endif; ?>

      </div>

    </div>

</div>

<?php
// Обработка добавления в корзину (простой пример)
if (isset( $_GET['add_to_cart']) && isset( $_SESSION['user'])) {
    $id_to_add = (int)$_GET['add_to_cart'];
    if (!isset( $_SESSION['cart'])) {
        $_SESSION['cart'] = [];
    }
    if (!isset( $_SESSION['cart'][$id_to_add])) {
        $_SESSION['cart'][$id_to_add] = 1;
    } else {
        $_SESSION['cart'][$id_to_add]++;
    }
    echo '<script>alert("Товар добавлен в корзину!"); window.location.href = "cart.php";</script>';
}
?>

</body>
</html>