36551-vm/shopping.php
2025-12-01 16:18:35 +00:00

132 lines
5.6 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
// Handle item deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['item_id_to_delete'])) {
$itemIdToDelete = $_POST['item_id_to_delete'];
try {
$pdo = db();
$stmt = $pdo->prepare("DELETE FROM shopping_items WHERE id = ?");
$stmt->execute([$itemIdToDelete]);
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
}
}
// Handle item purchase status update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['item_id_to_toggle'])) {
$itemIdToToggle = $_POST['item_id_to_toggle'];
try {
$pdo = db();
$stmt = $pdo->prepare("UPDATE shopping_items SET is_purchased = !is_purchased WHERE id = ?");
$stmt->execute([$itemIdToToggle]);
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
}
}
// Handle new item creation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['item_name'])) {
$itemName = trim($_POST['item_name']);
$quantity = trim($_POST['quantity']);
if (!empty($itemName)) {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO shopping_items (item_name, quantity) VALUES (?, ?)");
$stmt->execute([$itemName, $quantity]);
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
}
}
}
// Fetch all shopping items
$items = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, item_name, quantity, is_purchased FROM shopping_items ORDER BY is_purchased ASC, created_at DESC");
$items = $stmt->fetchAll();
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping List</title>
<link href="https://fonts.googleapis.com/css2?family=Lora:wght@400;700&family=Nunito:wght@400;700&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
font-family: 'Nunito', sans-serif;
background-color: #FFFBEB;
color: #374151;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Lora', serif;
}
.purchased {
text-decoration: line-through;
color: #9CA3AF;
}
</style>
</head>
<body class="flex flex-col items-center justify-center min-h-screen">
<div class="w-full max-w-2xl mx-auto bg-white rounded-xl shadow-lg p-6">
<div class="mb-6">
<h1 class="text-4xl font-bold text-center text-gray-800">Shopping List</h1>
<p class="text-center text-gray-500">Manage your shopping lists.</p>
</div>
<div class="mb-6">
<h2 class="text-2xl font-bold mb-4">Add New Item</h2>
<form method="POST" action="shopping.php" class="flex gap-4">
<input type="text" name="item_name" required class="flex-grow rounded-md border-gray-300 shadow-sm focus:border-yellow-500 focus:ring-yellow-500 sm:text-sm p-2" placeholder="Item Name">
<input type="text" name="quantity" class="rounded-md border-gray-300 shadow-sm focus:border-yellow-500 focus:ring-yellow-500 sm:text-sm p-2" placeholder="Quantity">
<button type="submit" class="px-4 py-2 bg-yellow-500 text-white font-bold rounded-md hover:bg-yellow-600">Add</button>
</form>
</div>
<div>
<h2 class="text-2xl font-bold mb-4">Your Shopping List</h2>
<div class="space-y-3">
<?php if (empty($items)): ?>
<p class="text-gray-500">No items in your shopping list yet.</p>
<?php else: ?>
<?php foreach ($items as $item): ?>
<div class="p-3 bg-gray-50 rounded-lg shadow flex justify-between items-center">
<div class="flex items-center">
<form method="POST" action="shopping.php" class="mr-3">
<input type="hidden" name="item_id_to_toggle" value="<?php echo $item['id']; ?>">
<input type="checkbox" <?php echo $item['is_purchased'] ? 'checked' : ''; ?> onchange="this.form.submit()" class="h-5 w-5 rounded border-gray-300 text-yellow-500 focus:ring-yellow-500">
</form>
<div class="<?php echo $item['is_purchased'] ? 'purchased' : ''; ?>">
<span class="font-bold"><?php echo htmlspecialchars($item['item_name']); ?></span>
<?php if (!empty($item['quantity'])): ?>
<span class="text-sm">(<?php echo htmlspecialchars($item['quantity']); ?>)</span>
<?php endif; ?>
</div>
</div>
<form method="POST" action="shopping.php">
<input type="hidden" name="item_id_to_delete" value="<?php echo $item['id']; ?>">
<button type="submit" class="text-red-500 hover:text-red-700 font-semibold">Delete</button>
</form>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<div class="text-center mt-8">
<a href="index.php" class="text-yellow-500 hover:underline">Back to Home</a>
</div>
</div>
</body>
</html>