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


<?php
session_start();

// Пример данных категорий
$categories = [
    ['id' => 1, 'name' => 'Мониторы', 'image' => 'https://via.placeholder.com/200x150?text=Мониторы'],
    ['id' => 2, 'name' => 'Мыши', 'image' => 'https://via.placeholder.com/200x150?text=Мыши'],
    ['id' => 3, 'name' => 'Клавиатуры', 'image' => 'https://via.placeholder.com/200x150?text=Клавиатуры'],
    ['id' => 4, 'name' => 'Наушники', 'image' => 'https://via.placeholder.com/200x150?text=Наушники']
];

// Обработка поиска и сортировки
$search = $_GET['search'] ?? '';
$sort = $_GET['sort'] ?? 'name';

// Здесь будет запрос к БД с фильтрацией и сортировкой
// Упрощённый пример данных товаров
$products = [
    ['id' => 1, 'name' => 'Монитор AOC 24"', 'price' => 15990, 'category_id' => 1],
    ['id' => 2, 'name' => 'Игровая мышь Razer', 'price' => 4990, 'category_id' => 2],
    ['id' => 3, 'name' => 'Клавиатура Logitech', 'price' => 3490, 'category_id' => 3],
    ['id' => 4, 'name' => 'Наушники Sony WH-1000', 'price' => 24990, 'category_id' => 4]
];
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Интернет‑магазин электроники</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            color: #333;
        }
        .header {
            background: #2c3e50;
            color: white;
            padding: 1rem 0;
            text-align: center;
        }
        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .search-sort {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
            align-items: center;
        }
        .search-box {
            flex: 1;
            display: flex;
            border: 1px solid #ddd;
            border-radius: 4px;
            overflow: hidden;
        }
        .search-box input {
            flex: 1;
            padding: 10px 15px;
            border: none;
            outline: none;
        }
        .search-box button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 15px;
            cursor: pointer;
        }
        .sort-select {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background: white;
        }
        .categories {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        .category-card {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            text-decoration: none;
            color: #333;
            transition: transform 0.3s;
        }
        .category-card:hover {
            transform: translateY(-5px);
        }
        .category-image {
            width: 100%;
            height: 150px;
            object-fit: cover;
        }
        .category-name {
            text-align: center;
            padding: 15px;
            font-weight: bold;
        }
        .products {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }
        .product-card {
            background: white;
            border-radius: 8px;
            padding: 15px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .product-name {
            font-weight: bold;
            margin-bottom: 10px;
        }
        .product-price {
            color: #e74c3c;
            font-weight: bold;
            margin-bottom: 10px;
        }
        .btn {
            display: inline-block;
            background: #27ae60;
            color: white;
            padding: 8px 15px;
            text-decoration: none;
            border-radius: 4px;
            text-align: center;
        }
        .cart-link {
            position: fixed;
            top: 20px;
            right: 20px;
            background: #e74c3c;
            color: white;
            padding: 10px 15px;
            text-decoration: none;
            border-radius: 4px;
            z-index: 100;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Интернет‑магазин электроники</h1>
    </div>

    <?php if (isset($_SESSION['user'])): ?>
        <a href="cart.php" class="cart-link">Корзина (<span id="cart-count">0</span>)</a>
    <?php endif; ?>

    <div class="container">
        <!-- Поисковая строка и сортировка -->
        <form method="GET" class="search-sort">
            <div class="search-box">
                <input type="text" name="search" placeholder="Поиск товаров..." value="<?= htmlspecialchars($search) ?>">
                <button type="submit">Найти</button>
            </div>
            <select name="sort" class="sort-select" onchange="this.form.submit()">
                <option value="name" <?= $sort === 'name' ? 'selected' : '' ?>>По названию</option>
                <option value="price_asc" <?= $sort === 'price_asc' ? 'selected' : '' ?>>Цена: по возрастанию</option>
                <option value="price_desc" <?= $sort === 'price_desc' ? 'selected' : '' ?>>Цена: по убыванию</option>
            </select>
        </form>

        <!-- Карточки категорий -->
        <div class="categories">
            <?php foreach ($categories as $category): ?>
                <a href="products.php?category_id=<?= $category['id'] ?>" class="category-card">
                    <img src="<?= $category['image'] ?>" alt="<?= htmlspecialchars($category['name']) ?>" class="category-image">
                    <div class="category-name"><?= htmlspecialchars($category['name']) ?></div>
                </a>
            <?php endforeach; ?>
        </div>

        <!-- Список товаров -->
        <h2>Популярные товары</h2>
        <div class="products">
            <?php foreach ($products as $product): ?>
                <div class="product-card">
                    <div class="product-name"><?= htmlspecialchars($product['name']) ?></div>
                    <div class="product-price"><?= number_format($product['price'], 0, '', ' ') ?> ₽</div>
                    <a href="product.php?id=<?= $product['id'] ?>" class="btn">Подробнее</a>
                </div