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 ?>

Annual Budget

num_rows > 0) { while($category = $categories->fetch_assoc()) { echo ""; echo ""; $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 ""; } else { echo ""; } } echo ""; echo ""; } // Displaying the monthly totals row echo ""; $grandTotal = 0; for ($month = 1; $month <= 12; $month++) { $grandTotal += $monthlyTotals[$month]; echo ""; } echo ""; echo ""; } ?>
Category January February March April May June July August September October November December Total
" . nullable_htmlentities($category['category_name']) . "" . $amount . "0" . $categoryTotal . "
Total" . $monthlyTotals[$month] . "" . $grandTotal . "