Загрузка данных
<?php
$products = [
[
'id' => 1,
'name' => 'Пицца Маргарита',
'category' => 'Пицца',
'description' => 'Классическая пицца с моцареллой, спелыми томатами и базиликом',
'price' => 590,
'oldPrice' => 690,
'image' => 'https://avatars.mds.yandex.net/i?id=9c7b8c21937681becdc058d650849d59518c8fc9-12190868-images-thumbs&n=13',
'inStock' => true,
'weight' => 450,
'calories' => 850,
],
[
'id' => 2,
'name' => 'Бургер Классик',
'category' => 'Бургеры',
'description' => 'Сочная говяжья котлета, сыр чеддер, маринованные огурчики и фирменный соус',
'price' => 420,
'oldPrice' => 490,
'image' => 'https://avatars.mds.yandex.net/i?id=bf024384b6294daecb88150e875463a755a23534-10555250-images-thumbs&n=13',
'inStock' => true,
'weight' => 320,
'calories' => 720,
],
[
'id' => 3,
'name' => 'Роллы Филадельфия',
'category' => 'Роллы',
'description' => 'Нежный сливочный сыр, свежий огурец и много слабосоленого лосося',
'price' => 650,
'oldPrice' => null,
'image' => 'https://avatars.mds.yandex.net/i?id=cf30525a624f5289d74e81141065bdd0a2a42ffa-4034522-images-thumbs&n=13',
'inStock' => false,
'weight' => 280,
'calories' => 540,
],
[
'id' => 4,
'name' => 'Паста Карбонара',
'category' => 'Горячее',
'description' => 'Спагетти с обжаренным беконом в нежном сливочно-сырном соусе с пармезаном',
'price' => 480,
'oldPrice' => 550,
'image' => 'https://avatars.mds.yandex.net/i?id=487d14ad92d80323ece81accb7f550d8_l-8803246-images-thumbs&n=13&n=13&w=345&h=230',
'inStock' => true,
'weight' => 350,
'calories' => 680,
],
[
'id' => 5,
'name' => 'Салат Цезарь',
'category' => 'Салаты',
'description' => 'Обжаренное куриное филе, листья романо, черри, сухарики и соус Цезарь',
'price' => 390,
'oldPrice' => 450,
'image' => 'https://avatars.mds.yandex.net/i?id=0b3ce0c62e927b93f499c1d6153ebdf9_l-4377564-images-thumbs&n=13',
'inStock' => true,
'weight' => 230,
'calories' => 410,
],
];
$available = array_filter($products, fn($p) => $p['inStock']);
$withDiscount = array_map(function ($p) {
$p['discount'] = $p['oldPrice']
? round(($p['oldPrice'] - $p['price']) / $p['oldPrice'] * 100)
: null;
return $p;
}, $products);
$categories = array_unique(array_column($products, 'category'));
$avgCalories = round(array_sum(array_column($products, 'calories')) / count($products));
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Доставка еды — Каталог блюд</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
color: #222;
}
.catalog-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
max-width: 800px;
}
.card {
border: 1px solid #ddd;
border-radius: 12px;
padding: 16px;
background: #fff;
}
.card-disabled {
opacity: 0.5;
}
.card-img {
width: 50%;
border-radius: 8px;
}
.description {
color: gray;
margin: 4px 0 8px 0;
font-size: 14px;
}
.meta-info {
font-size: 14px;
margin: 8px 0;
}
.price {
color: #e53935;
font-size: 20px;
}
.old-price {
color: gray;
margin-left: 6px;
}
.discount-badge {
background: #e53935;
color: white;
padding: 2px 6px;
border-radius: 4px;
font-size: 12px;
margin-left: 6px;
}
.status-ok {
color: green;
}
.status-no {
color: red;
}
.buy-btn {
width: 100%;
padding: 10px;
background: #1976d2;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 15px;
}
.buy-btn:disabled {
cursor: not-allowed;
}
</style>
</head>
<body>
<h2>Статистика каталога</h2>
<p>Всего блюд: <b><?= count($products) ?></b></p>
<p>В наличии: <b><?= count($available) ?></b></p>
<p>Категории: <b><?= implode(', ', $categories) ?></b></p>
<p>Средняя калорийность: <b><?= $avgCalories ?> ккал</b></p>
<hr>
<h2>Меню доставки</h2>
<div class="catalog-grid">
<?php foreach ($withDiscount as $product): ?>
<article class="card <?= $product['inStock'] ? '' : 'card-disabled' ?>">
<img src="<?= $product['image'] ?>" alt="<?= htmlspecialchars($product['name']) ?>" class="card-img">
<h3><?= htmlspecialchars($product['name']) ?></h3>
<p class="description"><?= htmlspecialchars($product['description']) ?></p>
<p class="meta-info">
Вес: <b><?= $product['weight'] ?> г</b> | Калорийность: <b><?= $product['calories'] ?> ккал</b>
</p>
<div>
<b class="price"><?= number_format($product['price'], 0, '.', ' ') ?> ₽</b>
<?php if ($product['oldPrice']): ?>
<s class="old-price"><?= number_format($product['oldPrice'], 0, '.', ' ') ?> ₽</s>
<span class="discount-badge">-<?= $product['discount'] ?>%</span>
<?php endif; ?>
</div>
<p class="<?= $product['inStock'] ? 'status-ok' : 'status-no' ?>">
<?= $product['inStock'] ? '✓ В наличии' : '✗ Нет в наличии' ?>
</p>
<button class="buy-btn" <?= $product['inStock'] ? '' : 'disabled' ?>>
В корзину
</button>
</article>
<?php endforeach; ?>
</div>
</body>
</html>