【问题标题】:Trouble printing increment inside html table无法在 html 表中打印增量
【发布时间】:2014-02-23 08:11:08
【问题描述】:

我在使用 ++ 增量来打印我的表格时遇到了一些问题。它目前打印出每一行的标题,如下所示:

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

这是代码:

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++;
}

【问题讨论】:

  • 因为您的 &lt;table&gt;&lt;th&gt; 在 while 循环内...将它们移到循环外。
  • 你在循环内只需要&lt;tr&gt;&lt;td&gt;" . $year . "&lt;/td&gt;&lt;td&gt;$currency" . number_format($current_owed, 0) . "&lt;/td&gt;&lt;/tr&gt; 其余的应该在外面

标签: php html html-table increment


【解决方案1】:

是的,因为您在 while 循环内打印&lt;th&gt;Year&lt;/th&gt; &lt;th&gt;Amount owed&lt;/th>,这意味着您打印每一行的表头。放

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

在循环之前应该没问题

【讨论】:

    【解决方案2】:

    帮自己一个忙,并格式化您的代码,以便更易于阅读和理解。让调试这样的小问题变得更加容易。

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

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2014-09-02
      • 2010-10-27
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      相关资源
      最近更新 更多