1

For loops needed to populate table. But I keep getting an extra two columns that repeat the data in the first two columns. What am I getting wrong with my for loops? All help is appreciated.

<!DOCTYPE html>  
        <html>
            <head>
            <meta charset="utf-8">
            <title>Calculating Interest</title>
            <style>
                table {
                    border:       3px solid black;
                    border-color: black;
                    text-align:   left;
                    padding:      1px;
                }
                th {
                    background-color: #01008A;
                    color: white;
                }
                tr:nth-child(odd) {background-color: #AED8E6}

            </style>
            </head>
        <body>  
            <table>
                <tr>
                    <th>Year</th>
                    <th>Amount on deposit</th>
                </tr>
                <?php  


                    for($i = 1; $i <= 10; $i++) { 
                        echo "<tr>";  
                        for ($j = 1; $j <= 2; $j++) {
                            $p = 1000;
                            $r = 0.05;
                            $n = 1;

                            echo "<td>" . $i . "</td>"; 
                            echo "<td>" . ($p = number_format(($p * pow((1 + $r), $i)), 2)) . "</td>";
                            $n++;
                        }  
                        echo "</tr>";  
                    }  
                ?>  
            </table>
        </body>  
    </html>  
3
  • 1
    because of nested for loop Commented May 30, 2017 at 4:43
  • Remove this loop for ($j = 1; $j <= 2; $j++ Commented May 30, 2017 at 4:45
  • Change line 32 to for ($j = 1; $j < 2; $j++) { Commented May 30, 2017 at 4:46

1 Answer 1

1

You have loop with $j that no needed! So use this:

<?php  
    for($i = 1; $i <= 10; $i++) { 
        echo "<tr>";  
        $p = 1000;
        $r = 0.05;
        $n = 1;

        echo "<td>" . $i . "</td>"; 
        echo "<td>" . ($p = number_format(($p * pow((1 + $r), $i)), 2)) . "</td>";
        $n++;
        echo "</tr>";  
    }  
?>  
Sign up to request clarification or add additional context in comments.

Comments

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.