/
Evil331
/
Apteka
Обзор
Документация
Войти
/
Evil331
/
Apteka
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
cart.php
137 строк
6 KB
Evil6766
Add files via upload
31 май 2025, 17:06
Не верифицирован
31 май 2025, 17:06
667fe25
Код
Авторство
О чём код?
<?php session_start(); require_once "db_connect.php"; $cart_items = []; $total = 0; if (isset($_SESSION['user_id'])) { $user_id = $_SESSION['user_id']; $stmt = $conn->prepare(" SELECT c.product_id, c.quantity, p.name, p.price, p.image_url FROM cart c JOIN products p ON c.product_id = p.id WHERE c.user_id = ? "); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $cart_items[] = $row; $total += $row['price'] * $row['quantity']; } $stmt->close(); } else { // Для неавторизованных пользователей используем session_id $session_id = session_id(); $stmt = $conn->prepare(" SELECT c.product_id, c.quantity, p.name, p.price, p.image_url FROM cart c JOIN products p ON c.product_id = p.id WHERE c.session_id = ? "); $stmt->bind_param("s", $session_id); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $cart_items[] = $row; $total += $row['price'] * $row['quantity']; } $stmt->close(); } ?> <!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Корзина - Интернет-аптека</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div class="logo"><a href="index.php">Y-Apteka</a></div> <div class="search-bar"> <input type="text" id="searchInput" placeholder="Поиск лекарств..."> <button id="searchButton" onclick="handleSearch()"> <img src="assets/images/search-icon.png" alt="Поиск"> </button> </div> <div class="cart"> <?php if (isset($_SESSION["username"])) { echo '<a href="profile.php">' . htmlspecialchars($_SESSION["username"]) . '</a> | <a href="logout.php">Выйти</a>'; } else { echo '<a href="login.php">Вход</a> | <a href="register.php">Регистрация</a>'; } ?> | <a href="cart.php" id="cartLink">Корзина (0)</a> </div> </header> <div class="nav-menu"> <ul> <li><a href="index.php">Главная</a></li> <li><a href="catalog.php">Каталог</a></li> <li><a href="cart.php">Корзина</a></li> <li><a href="about.php">О нас</a></li> <li><a href="contacts.php">Контакты</a></li> </ul> </div> <main class="container"> <h2>Ваша корзина</h2> <?php if (empty($cart_items)): ?> <p class="empty-cart">Корзина пуста. <a href="catalog.php">Перейти в каталог</a></p> <?php else: ?> <div class="cart-container"> <div class="cart-items"> <?php foreach ($cart_items as $item): ?> <div class="cart-item" data-id="<?php echo htmlspecialchars($item['product_id']); ?>"> <img src="<?php echo htmlspecialchars($item['image_url']); ?>" alt="<?php echo htmlspecialchars($item['name']); ?>" class="cart-item-image"> <div class="cart-item-details"> <h3><?php echo htmlspecialchars($item['name']); ?></h3> <p>Цена: <?php echo number_format($item['price'], 2); ?> RUB</p> <div class="quantity-control"> <button class="decrease-quantity" data-id="<?php echo htmlspecialchars($item['product_id']); ?>">-</button> <input type="number" value="<?php echo htmlspecialchars($item['quantity']); ?>" min="1" class="quantity-input" data-id="<?php echo htmlspecialchars($item['product_id']); ?>"> <button class="increase-quantity" data-id="<?php echo htmlspecialchars($item['product_id']); ?>">+</button> </div> </div> <div class="cart-item-total"> Итого: <?php echo number_format($item['price'] * $item['quantity'], 2); ?> RUB </div> <button class="remove-item" data-id="<?php echo htmlspecialchars($item['product_id']); ?>">Удалить</button> </div> <?php endforeach; ?> </div> <div class="cart-summary"> <h3>Итоговая сумма</h3> <p>Общая стоимость: <span id="cart-total"><?php echo number_format($total, 2); ?> RUB</span></p> <button id="checkout-button" class="checkout-button">Оформить заказ</button> </div> </div> <?php endif; ?> </main> <footer class="footer"> <div class="footer-content"> <div class="footer-links"> <a href="about.php">О нас</a> <a href="contacts.php">Контакты</a> <a href="#">Политика конфиденциальности</a> </div> <div class="footer-info"> <p>© 2025 Y-Apteka. Все права защищены.</p> <p>Телефон: +7 (959) 000-55-77</p> <p>Email: Y-apteka@sobaka.gav</p> </div> </div> </footer> <script src="script.js"></script> </body> </html>