求和值,然后添加到表中并保持求和?PHP
基本上,我想做到的是: 我有一个页面要求求和值,然后添加到表中并保持求和?PHP,php,html,Php,Html,基本上,我想做到的是: 我有一个页面要求 本金 利息金额 年份变量 我遇到的问题是本金和利息的数额 当用户点击submit时,它会根据year变量创建一个表。例如 如果年份变量为==到10,则表格有10行。 然后,第2列将显示用户输入的本金金额,第3列将显示用户输入的利率 但我的问题是,该表对所有行反复显示相同的信息 我需要第二行、第三行、第四行等来显示本金金额+利息金额 <?php $_SESSION['intRateSession'] = $intRate; $_SESSION['to
<?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>
第二行应显示上一行到底部的本金金额和利息金额之和 试试这个:
$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
}
$amounts=array();
$startAmount=1000000;//代码中没有给出?
对于($i=0;$i<$ytd;++$i)
{
$startAmount=$startAmount*$interestRate+$startAmount;
$amounts[]=数组(“年”=>$i+1,
“金额”=>$startAmount,
“利息”=>$interestRate
};
}
foreach(金额为$amount)
{
?>
试试这个:
$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
}
$amounts=array();
$startAmount=1000000;//代码中是否未给出?
对于($i=0;$i<$ytd;++$i)
{
$startAmount=$startAmount*$interestRate+$startAmount;
$amounts[]=数组(“年”=>$i+1,
“金额”=>$startAmount,
“利息”=>$interestRate
};
}
foreach(金额为$amount)
{
?>
这段代码很难帮上忙。下一次,试着画一个完整的圆圈,显示问题涉及的代码的每个部分。例如:$prinAmount
变量声明在哪里?
从我的角度来看,变量的声明是静态的,因为它们没有改变,所以应该反复显示相同的信息。
尝试将$\u会话['totalAmountSession']
declaration移动到FOR循环内部,每次迭代都会递增。类似于:
$\u会话['totalAmountSession']=$\u会话['totalAmountSession']+($prinAmount*$intRate);
请确保您使用的是数字。祝您好运!这段代码很难帮助您解决问题。下次,请尝试完整地循环显示问题涉及的代码的每个部分。例如:$prinAmount
变量声明在何处?
从我的角度来看,变量的声明是静态的,因为它们没有改变,所以应该反复显示相同的信息。
尝试将$\u会话['totalAmountSession']
declaration移动到FOR循环内部,每次迭代都会递增。类似于:
$\u会话['totalAmountSession']=$\u会话['totalAmountSession']+($prinAmount*$intRate);
确保您正在处理数字。祝您好运!prinAmount
从何而来?在二维数组中构建所有信息,然后将该数组输入代码以显示它。通常,混合计算[业务逻辑]使用显示逻辑会使代码混乱且难以维护。$prinAmount
从何而来?在二维数组中构建所有信息,然后将该数组输入代码以显示它。通常,混合计算[业务逻辑]显示逻辑使您的代码混乱且难以维护。这非常有效,我只需更改$startAmount的公式,但这一切都是真的。再次感谢!这非常有效,我只需更改$startAmount的公式,但这一切都是真的。再次感谢!