35900-vm/dashboard.php
2025-11-21 12:22:08 +00:00

134 lines
5.0 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$food_log_data = [];
$error_message = '';
$total_foods = 0;
$total_calories = 0;
$calories_by_food = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT food_name, calories, sale_date FROM food_log ORDER BY sale_date DESC");
$food_log_data = $stmt->fetchAll();
if ($food_log_data) {
$total_foods = count($food_log_data);
$total_calories = array_sum(array_column($food_log_data, 'calories'));
foreach ($food_log_data as $food) {
if (!isset($calories_by_food[$food['food_name']])) {
$calories_by_food[$food['food_name']] = 0;
}
$calories_by_food[$food['food_name']] += $food['calories'];
}
arsort($calories_by_food);
}
} catch (PDOException $e) {
$error_message = "Error fetching food log data: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - Calorie Tracker</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="mobile-container">
<header class="app-header">
<a href="index.php" class="back-button">&larr;</a>
<h1>Dashboard</h1>
</header>
<main class="main-content">
<?php if ($error_message): ?>
<div class="alert error">
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php elseif (empty($food_log_data)) : ?>
<div class="alert">
No food logged yet. Add a food item to see your stats here.
</div>
<?php else: ?>
<div class="stats-summary">
<div class="summary-card">
<span class="value"><?php echo $total_foods; ?></span>
<span class="label">Total Foods</span>
</div>
<div class="summary-card">
<span class="value"><?php echo $total_calories; ?></span>
<span class="label">Total Calories</span>
</div>
</div>
<h2 class="h5 mt-4 mb-3 text-white">Calories by Food</h2>
<div class="table-responsive">
<table class="table sales-table">
<thead>
<tr>
<th>Food</th>
<th>Total Calories</th>
</tr>
</thead>
<tbody>
<?php foreach ($calories_by_food as $food_name => $calories): ?>
<tr>
<td><?php echo htmlspecialchars($food_name); ?></td>
<td><?php echo htmlspecialchars((string)$calories); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<h2 class="h5 mt-4 mb-3 text-white">Recent Entries</h2>
<div class="table-responsive">
<table class="table sales-table">
<thead>
<tr>
<th>Food</th>
<th>Calories</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($food_log_data as $food): ?>
<tr>
<td><?php echo htmlspecialchars($food['food_name']); ?></td>
<td><?php echo htmlspecialchars((string)$food['calories']); ?></td>
<td><?php echo htmlspecialchars(date("M d, Y H:i", strtotime($food['sale_date']))); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</main>
</div>
<!-- Bottom Navigation -->
<nav class="bottom-nav">
<a href="index.php" class="nav-item">
<i class="bi bi-house-door-fill"></i>
<span>Home</span>
</a>
<a href="add_food.php" class="nav-item">
<i class="bi bi-plus-circle-fill"></i>
<span>Add Food</span>
</a>
<a href="dashboard.php" class="nav-item active">
<i class="bi bi-bar-chart-line-fill"></i>
<span>Dashboard</span>
</a>
</nav>
</body>
</html>