Загрузка данных
<?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';
$category_id = $_GET['category_id'] ?? null;
// Упрощённый пример данных товаров
$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],
['id' => 5, 'name' => 'Монитор Samsung', 'price' => 18990, 'category_id' => 1],
['id' => 6, 'name' => 'Мышь A4Tech', 'price' => 1990, 'category_id' => 2]
];
// Фильтрация по категории и поиску
$filtered_products = array_filter( $products, function( $product) use ( $category_id, $search) {
$match_category = ( $category_id === null) || ( $product['category_id'] == $category_id);
$match_search = (stripos( $product['name'], $search) !== false);
return $match_category && $match_search;
});
// Сортировка
usort( $filtered_products, function( $a, $b) use ( $sort) {
if ( $sort === 'name') {
return strcasecmp( $a['name'], $b['name']);
} elseif ( $sort === 'price_asc') {
return $a['price'] <=> $b['price'];
} elseif ( $sort === 'price_desc') {
return $b['price'] <=> $a['price'];
}
return 0;
});
?>
<!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 {
background-color:#27ae60; /* Исправлено */
color:#fff; /* Исправлено */
padding:8px 15px; /* Исправлено */
text-decoration:none; /* Исправлено */
border-radius:4px; /* Исправлено */
display:inline-block; /* Исправлено */
text-align:center; /* Исправлено */
margin-top:-38px; /* Для красоты */
}
.btn:focus,
.btn:focus-visible { outline:none; box-shadow:none; }
.btn:focus,
.btn:focus-visible,
.btn:focus-within { outline:none; box-shadow:none; }
.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="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="?category_id=<?= $category['id'] ?>" class="category-card">
<img alt="<?= htmlspecialchars( $category['name']) ?>" alt="<?= htmlspecialchars( $category['name']) ?>" class="category-image">
<div class="category-name"><?= htmlspecialchars( $category['name']) ?></div>
</a>
<?php endforeach; ?>
</div>
<!-- Список товаров -->
<h2>Популярные товары</h2>
<?php if (count( $filtered_products) > 0) : ?>
<div class="products">
<?php foreach ( $filtered_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>
<?php endforeach; ?>
</div>
<?php else : ?>
<p>Товаров не найдено.</p>
<?php endif; ?>
</div>
</body>
</html>