一组文本字段的总值无法显示/计算-angular js-html.php



我有一个简单的表单,它有"total"字段和另一个整体total字段(这是所有"totall"字段的总和)。

"合计"字段是根据数量和单位成本字段的值计算的,即qty0*unitcost0=total0,qty1*unitcost1=total1,依此类推。请注意,数量和amt来自数据库

我希望总的总和像total0+total1+total2……那样实时计算。

这是我的代码(很抱歉,如果这段代码对一些人来说很乱,但我只是编程的初学者)我的ng应用程序在body标签中,我的ng控制器在div 中

<?php
echo '
<form name="editpo" id="editpo" method="post" action="poeditprocess.php">
<table>
<tr>
<th colspan="1">Stock No.</th>
<th colspan="1"> Unit </th>
<th colspan="5">Description</th>
<th colspan="1">Quantity</td>
<th colspan="1">Unit Cost</th>
<th colspan="1">Amount</th>
</tr>
</table>
<table id="items">';

//print the item desc, unit, qty and unit cost 
$ctr02 = 1;
$tamt = 0;
for ($i=0; $i < $ctritem; $i++)
{
$amt = $qty[$i] * $unitc[$i];
$tamt = $tamt + $amt;
$unitc[$i] = number_format($unitc[$i], 2, '.', ',');
$amt = number_format($amt, 2, '.', ',');
//initialize the qty and unitcost with the values fro db                
echo '<tr ng-init="qty'.$i.'='.$qty[$i].'; cost'.$i.'='.$unitc[$i].';">';
echo "
<td id='amt'>".$ctr02."</td>
<td> <input type='text' name='unit[".$i."]' value='".$poitemunit[$i]."' required/></td>
<td colspan='3'><textarea rows='2' name='desc[".$i."]' required>".$poitemdesc[$i]."</textarea></td>";
echo '
<td><input type="number" name="qty'.$i.'"  id="qty'.$i.'" min="0" max="10000" ng-model="qty'.$i.'" required /></td>
<td><input type="number" name="unitcost" ng-model ="cost'.$i.'" step="any" min="0" required /></td>
<td><input type="text" name="amt'.$i.'" id="amt'.$i.'" step="any" value="{{(qty'.$i.' * cost'.$i.') | currency : 'P ' : 2}}" disabled/></td>
</tr>';
$ctr02++;
}
echo "
</table>";
echo "  <table>";
//this is the overall total
echo '<td><input type="text" name="tamt" id="tamt" value="{{(';
$x=0;
while($x<=$ctritem){
echo '(qty'.$x.' * cost'.$x.')';
if($x<$ctritem){
echo '+';
}
$x++;
}
echo ')| currency : 'Php ' : 2}}" disabled/></td>
</tr>';
echo "</table> 
</form>";
?>

ctrtitem是列表中的项目数。我用for循环打印所有要合计的项目。然后使用while循环来获得总和。

总数运行良好。但总的来说根本不起作用我不知道这出了什么问题,因为我在另一个页面上也有同样的代码。任何帮助都将不胜感激。提前感谢!

<td>
<input type="text" name="amt'.$i.'" id="amt'.$i.'" step="any" 
value="{{(qty'.$i.' * cost'.$i.') | currency : 'P ' : 2}}" 
disabled/>
</td>

声明变量

total$i = qty$i * cost$i 

在for循环中,并将previous行更改为然后在for循环外声明

$grandtotal = 0 

在for循环中,将$total$i添加到$grandtotal whith:

$grandtotal += $total$i;

@Leilo谢谢你的回答,我非常感谢。然而,我仍然无法使它工作。但经过多次尝试后,我想我会为grand-total重写整个代码片段,然后它就成功了。(在这里插入大笑,因为我真的不知道第一个代码出了什么问题)

因此,为了所有将遇到与我相同问题(或与我的问题密切相关)的人的利益,以下是代码:

//this is the overall total
echo '<td><input type="text" name="tamt" id="tamt" value="{{';
$ctrt = 0;      
while ($ctrt<$i)
{
echo'(qty'.$ctrt.' * cost'.$ctrt++.')';
if($ctrt<$i){
echo '+';
}
}   
echo '| currency : 'Php ' : 2}}" disabled/></td>';

最新更新