Загрузка данных
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Моделирование работы маркетплейса (PHP)</title>
<style>
* { box-sizing: border-box; }
body {
font-family: 'Segoe UI', Arial, sans-serif;
background: #b4b4b4;
color: #222;
margin: 0;
padding: 30px;
line-height: 1.5;
max-width: 1100px;
margin-left: auto;
margin-right: auto;
}
h1 { color: #000; margin-bottom: 20px; }
h2 { color: #050606; margin-top: 25px; margin-bottom: 15px; }
table {
width: 100%;
border-collapse: collapse;
background: #fff;
border-radius: 22px;
overflow: hidden;
box-shadow: 0 2px 6px rgba(0,0,0,0.08);
margin-bottom: 20px;
}
th {
background: #fc9bff;
color: #fff;
padding: 12px 15px;
text-align: left;
font-weight: 600;
}
td {
padding: 10px 15px;
border-bottom: 1px solid #eee;
}
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f9f9f9; }
form {
background: #fff;
padding: 18px;
border-radius: 15px;
box-shadow: 0 2px 6px rgba(0,0,0,0.08);
margin-bottom: 20px;
}
input, select {
border-radius: 16px;
padding: 8px 12px;
border: 1px solid #ccc;
font-family: inherit;
font-size: 14px;
}
button {
background: #fc9bff;
color: #fff;
border: none;
padding: 9px 18px;
border-radius: 16px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: background 0.4s;
}
button:hover { background: #a331ff; }
.sort-links { margin-top: 10px; }
.sort-links a { text-decoration: none; margin-right: 6px; }
</style>
</head>
<body>
<?php
function generateName() {
$alphabetLower = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
$alphabetUpper = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ';
$digits = '0123456789';
$name = mb_substr($alphabetUpper, mt_rand(0, mb_strlen($alphabetUpper) - 1), 1, 'UTF-8');
$length = mt_rand(5, 7);
for ($i = 1; $i < $length; $i++) {
if (mt_rand(0, 1) > 0.5) {
$name .= mb_substr($alphabetLower, mt_rand(0, mb_strlen($alphabetLower) - 1), 1, 'UTF-8');
} else {
$name .= $digits[mt_rand(0, strlen($digits) - 1)];
}
}
return $name;
}
function generateDescription() {
$words = ['крутой', 'удобный', 'стильный', 'качественный', 'дешевый', 'надежный', 'современный', 'быстрый', 'легкий', 'мощный', 'компактный'];
$description = '';
$wordCount = mt_rand(5, 7);
for ($i = 0; $i < $wordCount; $i++) {
$description .= $words[mt_rand(0, count($words) - 1)] . ' ';
}
return trim($description);
}
$count = isset($_GET['count']) ? (int)$_GET['count'] : 10;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'none';
if ($count < 1) $count = 1;
if ($count > 50) $count = 50;
$seed = isset($_GET['seed']) ? (int)$_GET['seed'] : mt_rand(1, 999999);
mt_srand($seed);
$brands = ['Samsung', 'Apple', 'Xiaomi', 'Huawei', 'Sony', 'LG', 'Asus'];
$products = array();
for ($i = 0; $i < $count; $i++) {
$products[] = array(
"Название" => generateName(),
"Цена" => mt_rand(100, 10100),
"Количество" => mt_rand(1, 50),
"Описание" => generateDescription(),
"Бренд" => $brands[mt_rand(0, count($brands) - 1)]
);
}
$random_index = mt_rand(0, count($products) - 1);
$random_product_name = $products[$random_index]['Название'];
$buy_quantity = mt_rand(0, 30);
foreach ($products as $key => $product) {
if ($product['Название'] == $random_product_name) {
$current_stock = $product['Количество'];
$remainder = $current_stock - $buy_quantity;
if ($remainder < 0) {
$products[$key]['Количество'] = $current_stock;
} else {
$products[$key]['Количество'] = $remainder;
}
break;
}
}
if ($sort === 'name') {
usort($products, function($a, $b) { return strcmp($a['Название'], $b['Название']); });
} elseif ($sort === 'quantity') {
usort($products, function($a, $b) { return $a['Количество'] - $b['Количество']; });
} elseif ($sort === 'price') {
usort($products, function($a, $b) { return $a['Цена'] - $b['Цена']; });
}
?>
<h1>Список товаров</h1>
<div class="controls">
<form method="GET">
<label>Сколько товаров вывести?
<input type="number" name="count" value="<?= $count ?>" min="1" max="50">
</label>
<button type="submit">Получить товары</button>
<br><br>
<label>Сортировка (список):
<select name="sort" onchange="this.form.submit()">
<option value="none" <?= $sort == 'none' ? 'selected' : '' ?>>Без сортировки</option>
<option value="name" <?= $sort == 'name' ? 'selected' : '' ?>>По названию</option>
<option value="quantity" <?= $sort == 'quantity' ? 'selected' : '' ?>>По количеству</option>
</select>
</label>
</form>
<div class="sort-links">
<a href="?count=<?= $count ?>&sort=name&seed=<?= $seed ?>"><button type="button">Сортировать по названию</button></a>
<a href="?count=<?= $count ?>&sort=price&seed=<?= $seed ?>"><button type="button">Сортировать по цене</button></a>
<a href="?count=<?= $count ?>&sort=quantity&seed=<?= $seed ?>"><button type="button">Сортировать по кол-ву</button></a>
</div>
</div>
<?php
echo "<h3>Случайная покупка:</h3>";
echo "<p>Выбран товар: <b>$random_product_name</b> в количестве: <b>$buy_quantity</b> шт.</p>";
?>
<h2>Таблица 1. Исходные данные</h2>
<table>
<thead>
<tr>
<th>Название</th>
<th>Цена</th>
<th>Кол-во</th>
<th>Описание</th>
<th>Бренд</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $product): ?>
<tr>
<td><?= $product['Название'] ?></td>
<td><?= $product['Цена'] ?> ₽</td>
<td><?= $product['Количество'] ?> шт.</td>
<td><?= $product['Описание'] ?></td>
<td><?= $product['Бренд'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Таблица 2. Остаток после попытки покупки</h2>
<table>
<thead>
<tr>
<th>Название</th>
<th>Цена</th>
<th>Кол-во</th>
<th>Описание</th>
<th>Бренд</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $product): ?>
<tr>
<td><?= $product['Название'] ?></td>
<td><?= $product['Цена'] ?> ₽</td>
<td><?= $product['Количество'] ?> шт.</td>
<td><?= $product['Описание'] ?></td>
<td><?= $product['Бренд'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>