<?php
include('../includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo('<h1>Monthly Budget Tracker</h1>');
// variables passed via POST ternary operators
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$income = isset($_POST['income']) ? $_POST['income'] : '';
$numExpenses = isset($_POST['numExpenses']) ? $_POST['numExpenses'] : '';
// filter input taken from browser
$step = (int)$step;
$income = htmlspecialchars($income);
$numExpenses = htmlspecialchars($numExpenses);
// first time viewing form
if ($step == 0) {
?>
<form action="budget_form.php" method="post">
<label>Monthly Income:</label><br>
<input type="text" name="income" value="<?php echo $income; ?>">
<br>
<label>How many expenses would you like to enter?</label><br>
<input type="text" name="numExpenses" value="<?php echo $numExpenses; ?>">
<br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else if ($step == 1) {
//cast
$income = (float)$income;
$numExpenses = (int)$numExpenses;
if($income <=0 || $numExpenses <=0) {
echo('<p class=c5>Please enter valid income and expense number.</p>');
echo('<a href="budget_form.php">Start Over</a>');
}
else
{
echo('<h2>Enter Monthly Expenses</h2>');
echo('<form action="budget_form.php" method="post">');
//loop for expense fields
for ($i = 1; $i <= $numExpenses; $i++) {
echo('<strong>Expense '.$i.'</strong><br>');
echo('Name:<br>');
echo('<input type="text" name="expenseName'.$i.'"><br>');
echo('Amount:<br>');
echo('<input type="text" name="expenseAmount'.$i.'"><br><br><hr>');
}
echo('<input type="hidden" name="income" value="'.$income.'">');
echo('<input type="hidden" name="numExpenses" value="'.$numExpenses.'">');
echo('<input type="hidden" name="step" value="2">');
echo('<input type="submit" name="submit" value="Calculate Budget">');
echo('</form>');
}
}
else if ($step == 2) {
$income = (float)$income;
$numExpenses = (int)$numExpenses;
$totalExpenses = 0;
echo('<h2>Monthly Budget Summary</h2>');
echo('<strong>Monthly Income:</strong> $'.number_format($income, 2, '.', ',').'<br><br>');
echo('<strong>Itemized Expenses:</strong><br>');
for ($i = 1; $i <= $numExpenses; $i++) {
$expenseNameKey = 'expenseName'.$i;
$expenseAmountKey = 'expenseAmount'.$i;
$name = isset($_POST[$expenseNameKey]) ? $_POST[$expenseNameKey] : '';
$amount = isset($_POST[$expenseAmountKey]) ? $_POST[$expenseAmountKey] : 0;
$name = htmlspecialchars($name);
$amount = (float)$amount;
$totalExpenses += $amount;
echo($name.': $'.number_format($amount, 2, '.', ',').'<br>');
}
echo('<br><strong>Total Monthly Expenses:</strong> $'.number_format($totalExpenses, 2, '.', ',').'<br>');
$remaining = $income - $totalExpenses;
echo('<strong>Remaining Balance:</strong> $'.number_format($remaining, 2, '.', ',').'<br><br>');
if ($remaining < 0) {
echo('<p class="c5">Status: You are OVER budget.</p>');
}
else if ($remaining == 0) {
echo('<p class="c4">Status: You broke even.</p>');
}
else {
echo('<p class="c1">Status: You are UNDER budget.</p>');
}
if ($income > 0) {
$savingsPercent = ($remaining / $income) * 100;
echo('<br>Savings Rate: '.number_format($savingsPercent, 1, '.', ',').'%<br>');
if ($savingsPercent >= 20) {
echo('<p class="c1">Excellent savings rate!</p>');
}
else if ($savingsPercent >= 10) {
echo('<p class="c4">Moderate savings rate.</p>');
}
else if ($savingsPercent > 0) {
echo('<p class="c5">Low savings rate.</p>');
}
}
echo('<br><a href="budget_form.php">Start Over</a>');
}
include('../includes/footer.php');
?>