基本上我想要完成的是: 我有一个页面要求
我遇到的问题是本金和利息金额。
当用户点击提交时,它会根据年份变量创建一个表格。例如,
如果年变量==到10,那么表有10行。 然后第二列将具有用户输入的本金金额,第三列将具有用户输入的利率。
但我的问题是,该表一遍又一遍地显示所有行的相同信息。
我需要有第二,第三,第四等行来显示本金额+利息金额。
<?php
$_SESSION['intRateSession'] = $intRate;
$_SESSION['totalAmountSession'] = $prinAmount*$intRate;
// Checks if submit is pressed, if so, grabs the $ytd variable to see how many
// times it loops (To create the amount of table rows);
if (isset($_POST['submit']))
{
for ($i = 1; $i <= $ytd; ++$i)
{
// Print out the amount of table rows based on the amount of years;
?>
<tr>
<th> <?php echo "Year " .$i; ?> </th>
<!-- *** Info to go in the principal amount at year start row *** -->
<!-- *** Trying to figure this out still *** -->
<th><?php echo $_SESSION['totalAmountSession']; ?></th>
<!-- *** Info to go in the interest row *** -->
<!-- *** Trying to figure this out still *** -->
<th><?php echo $_SESSION['intRateSession']; ?></th>
</tr>
<?php
}
}
?>
</table>
第二行应显示从前一行一直到底部的本金额和利息金额之和。
答案 0 :(得分:0)
试试这个:
$amounts = array();
$startAmount = 1000000; // not given in your code?
for ($i = 0; $i < $ytd; ++$i)
{
$startAmount = $startAmount * $interestRate + $startAmount;
$amounts[] = array( "year" => $i+1,
"amount" => $startAmount,
"interest" => $interestRate
};
}
foreach ( $amounts as $amount )
{
?>
<tr>
<td><?php echo $amount['year']; ?>
<td><?php echo $amount['interest']; ?>
<td><?php echo $amount['amount']; ?>
</tr>
<?php
}
答案 1 :(得分:0)
这段代码很难提供帮助。下次尝试完整圆圈,显示问题涉及的代码的每个部分。例如:在哪里声明了$prinAmount
变量?
从我的角度来看,变量的声明是静态的,预计它们会一遍又一遍地显示相同的信息,因为它们没有变化。
尝试移动$_SESSION['totalAmountSession']
声明内部 FOR循环每次迭代增加自身。就像是:
$_SESSION['totalAmountSession']=$_SESSION['totalAmountSession']+($prinAmount*$intRate);
确保您正在使用数字。祝你好运!