0

I'm having a little problem figuring out 'how' to print a 2nd variable within a loop. Basically, I'm trying to create a table that shows 'how many years' someone will save for retirement AND 'what their total savings' would be each year. I've never used a 'nested loop' before and I think this may be what I need to use.

I have the following code. It works by echoing the years saved as a loop, but I don't know what to do to echo the 'total savings' correctly. You'll notice that the first part of the number in "total savings" column is correct, but the variable is adding the previous value to the end of the number.

Thank you for your help!

The CODE I'm using is

<?php

//variables
$savings = $_POST["savings"];
$invested = $_POST["invested"];
$networth = $_POST["networth"];
$salary = $_POST["salary"];
$save_rate = $_POST["save_rate"];
$military = $_POST["military"];
$donate = $_POST["donate"];
$goods = $_POST["goods"];
$years_save = $_POST["years_save"];

//savings calculator
$annual_savings = $salary*$save_rate;
$total_savings = $savings + ($annual_savings*$years_save);


?>
</head>

<body>

<h1>Test Handler</h1><hr />
<table width="800" border="0" cellspacing="0" cellpadding="0">

  <tr>
    <td width="150">Years</td>
    <td width="450">Yearly Savings</td>
    <td width="200">Total Savings </td>
  </tr>
<?php
for ( $i=1; $i<=$years_save; $i++ ) {
            echo "<tr>
               <td>$i</td>
                <td>$annual_savings</td>
                <td>" . $totalsavings = $i*$annual_savings . "$totalsavings</td>
                </tr>";
                }

?>
</table>

Output is

Years   Yearly Savings  Total Savings
1   13500   13500
2   13500   2700013500
3   13500   405002700013500
4   13500   54000405002700013500
5   13500   6750054000405002700013500
6   13500   810006750054000405002700013500
7   13500   94500810006750054000405002700013500
8   13500   10800094500810006750054000405002700013500
9   13500   12150010800094500810006750054000405002700013500
10  13500   13500012150010800094500810006750054000405002700013500
11  13500   14850013500012150010800094500810006750054000405002700013500
12  13500   16200014850013500012150010800094500810006750054000405002700013500
1
  • Have you tried to move your calculations inside the loop, depending on the loop variable? Commented Dec 17, 2013 at 17:04

2 Answers 2

2

Try to make it like this:

<?php
for ( $i=1; $i<=$years_save; $i++ ) {
        $totalsavings = $i*$annual_savings;
        echo "<tr>
           <td>$i</td>
            <td>$annual_savings</td>
            <td>$totalsavings</td>
            </tr>";
            }

?>
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a very basic loop; it doesn't use all of your form values, but it's simplified and should help you implement what you're looking for. You could also do some additional math inside the loop:

$rate = 0.6;
$salary = 50000;
$years = 12;
$savings = 13500;
$base = $salary;

echo "<table>";
for ($i = 0; $i < $years; $i++)
{
    // do some basic math; calculate this however you like
    // += will add the math result to $base, leaving $salary available for additional calculations
    $base += $savings * $rate; 
    echo "<tr><td>Years: $i</td><td>Savings: $base</td></tr>";
}
echo "</table>";

1 Comment

R3mus, thank you for your answer. I appreciate the different way of looking at the problem. Thank you for introducing the += idea. I was not aware of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.