使用PHP for循环填充html表

时间:2017-05-30 04:41:44

标签: php html loops for-loop html-table

用于填充表所需的循环。但我继续得到额外的两列,重复前两列中的数据。我的for循环出了什么问题?感谢所有帮助。

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

1 个答案:

答案 0 :(得分:1)

你有$j的循环,不需要!所以使用这个:

<?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>";  
    }  
?>