exec($sql); } $stmt = $pdo->prepare("SELECT * FROM projects WHERE id = :id"); $stmt->execute([':id' => $projectId]); $project = $stmt->fetch(PDO::FETCH_ASSOC); if (!$project) { header("Location: projects.php"); exit(); } // Initialize expenses records if they don't exist $startDate = new DateTime($project['startDate']); $endDate = new DateTime($project['endDate']); $currentMonth = clone $startDate; while ($currentMonth <= $endDate) { $monthStr = $currentMonth->format('Y-m-01'); $stmt = $pdo->prepare("SELECT COUNT(*) FROM expensesMonthly WHERE projectId = :projectId AND month = :month"); $stmt->execute([':projectId' => $projectId, ':month' => $monthStr]); $count = $stmt->fetchColumn(); if ($count == 0) { $insertStmt = $pdo->prepare("INSERT INTO expensesMonthly (projectId, month, amount) VALUES (:projectId, :month, 0)"); $insertStmt->execute([':projectId' => $projectId, ':month' => $monthStr]); } $currentMonth->modify('+1 month'); } // Fetch expenses data $stmt = $pdo->prepare("SELECT * FROM expensesMonthly WHERE projectId = :projectId ORDER BY month"); $stmt->execute([':projectId' => $projectId]); $expensesData = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $db_error = "Database error: " . $e->getMessage(); } ?> Expenses - <?php echo htmlspecialchars($project['name']); ?>
' . $currentMonth->format('M Y') . ''; $currentMonth->modify('+1 month'); } ?> '; } ?>
Expenses
Expenses