Question

I'm having some trouble with using the ++ increment to print my table. It currently prints out the headers with every row like this:

Year    Amount owed
1   $234,500
Year    Amount owed
2   $218,473
Year    Amount owed
3   $201,901
...

This is the code:

while ($current_owed > 0) {
$current_owed = (($current_owed*$annual_interest)+$current_owed)-$yearly_payment; 
print "<table border=\"1\">
<tr>
<th>Year</th>
<th>Amount owed</th>
</tr>
<tr><td>" . $year . "</td><td>$currency" . number_format($current_owed, 0) . "</td>
</tr>
</table>";
$year++;
}
Was it helpful?

Solution

Yes, because you print <th>Year</th> <th>Amount owed</th> inside the while loop which means you print the header of table for each row. Put

<th>Year</th>
<th>Amount owed</th>

before the loop and should be okay

OTHER TIPS

Do yourself a favor and format your code so that is easier to read and understand. Makes debugging little issues like this a lot easier.

<table border="1">
    <tr>
        <th>Year</th>
        <th>Amount owed</th>
    </tr>
    <?php
    while ($current_owed > 0) {
        $current_owed = (($current_owed*$annual_interest)+$current_owed)-$yearly_payment; 
        ?>
        <tr>
            <td><?php echo $year ?></td>
            <td><?php echo $currency.number_format($current_owed, 0) ?></td>
        </tr>
        <?php
        $year++;
    }
    ?>
</table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top