0 ORDER BY expense_year DESC"); $categories = mysqli_query($mysqli, "SELECT * FROM categories WHERE category_type = 'Expense' ORDER BY category_name ASC"); $monthlyTotals = array_fill(1, 12, 0); // Initialize monthly totals for each month ?>
| Category | January | February | March | April | May | June | July | August | September | October | November | December | Total |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| " . nullable_htmlentities($category['category_name']) . " | "; $categoryTotal = 0; for ($month = 1; $month <= 12; $month++) { // Fetch the monthly budget for this category for 2022 $sql = "SELECT budget_amount FROM budget WHERE budget_category_id = " . $category['category_id'] . " AND budget_month = $month AND budget_year = $year"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { $budget = $result->fetch_assoc(); $amount = $budget['budget_amount']; $categoryTotal += $amount; $monthlyTotals[$month] += $amount; echo "" . $amount . " | "; } else { echo "0 | "; } } echo "" . $categoryTotal . " | "; echo "||||||||||
| Total | "; $grandTotal = 0; for ($month = 1; $month <= 12; $month++) { $grandTotal += $monthlyTotals[$month]; echo "" . $monthlyTotals[$month] . " | "; } echo "" . $grandTotal . " | "; echo "