我从这个表单(form.php)获得了一系列数据:
<?
include"connect.php";
$sql = "select * from tbsatpor ";
$qry = mysql_query($sql);
echo"<form name='submit' method='POST' action='proses.php'>";
$no = 1;
while ($data = mysql_fetch_array($qry)){
$nomor = $no++;
echo "<input type='text' name='kd_satpor[]' value='$data[kd_satpor]' >
<input type='text' name='kd_unor[]' value='$data[kd_unor]' >
<input type='text' name='buy[]' value=''><br>";
}
echo"<input type='submit' name='submit' value='submit'></form>";
?>
这是resultpage.php:
<?
echo "<table border='0' size=100%>
<tr><td>CODE</td><td>BUY</td><td>UNOR</td><td>TOTAL SUM</td></tr>";
for ($i=0; $i<count($_POST['buy']);$i++){
$_session[$i] = $_POST['kd_satpor'][$i];
$_session[$_POST['buy'][$i]] = $_POST['buy'][$i];
$_session[$_POST['kd_unor'][$i]] = $_POST['kd_unor'][$i];
$total[i] = $_session[$_POST['kd_unor'][$i]] * $_session[$_POST['buy'][$i]];
if($total[i]!=0){
echo "<tr><td>".$_session[$i]."</td><td>" .$_session[$_POST['buy'][$i]]." X
</td><td>".$_session[$_POST['kd_unor'][$i]]."=</td><td>".$total[i]."</td>
<td></tr>";
}
}
echo"</table>";
?>
数据数组看起来像这样(resultpage.php的输出):
CODE | BUY | UNOR | SUM
+++++++++++++++++++++++++++++++
024K30 | 10000 | 2 | 20000 | => 10000 x 2 = 20000
024K31 | 2000 | 3 | 6000 | => 2000 x 3 = 6000
024K32 | 5000 | 3 | 15000 | => 5000 x 3 = 15000
TOTAL?
我想从数组中创建TOTAL SUM并显示该代码的结果。我怎么能这样做?
答案 0 :(得分:1)
$finalTotal = 0;
for ($i=0; $i<count($_POST['buy']);$i++)
{
...
$finalTotal += $total[i];
}
echo 'Final Total: ' . $finalTotal;
此外,如果您打算使用PHP会话,请使用$_SESSION
而不是$_session
,并且在将其回显到页面之前不要忘记通过htmlentities()
运行任何POST数据。
答案 1 :(得分:0)
猜测你想要什么:
<?
$OverallTotal=0;
echo"<table border='0' size=100%>
<tr><td>CODE</td><td>BUY</td><td>UNOR</td><td>TOTAL SUM</td></tr>";
for ($i=0; $i<count($_POST['buy']);$i++)
{
$_session[$i]=$_POST['kd_satpor'][$i];
$_session[$_POST['buy'][$i]]=$_POST['buy'][$i];
$_session[$_POST['kd_unor'][$i]]=$_POST['kd_unor'][$i];
$total[i]=$_session[$_POST['kd_unor'][$i]]*$_session[$_POST['buy'][$i]];
$OverallTotal+=$total[i];
if($total[i]!=0)
{
echo "<tr><td>".$_session[$i]."</td><td>" .$_session[$_POST['buy'][$i]]." X </td><td>".$_session[$_POST['kd_unor'][$i]]."=</td><td>".$total[i]."</td><td></tr>";
}
}
echo"</table>";
echo $OverallTotal;
?>
答案 2 :(得分:0)
尝试使用递增运算符,如下所示:
<?php
$totalSum = 0;
echo"<table border='0' size=100%>
<tr><td>CODE</td><td>BUY</td><td>UNOR</td><td>TOTAL SUM</td></tr>";
for ($i=0; $i<count($_POST['buy']);$i++) {
$_session[$i]=$_POST['kd_satpor'][$i];
$_session[$_POST['buy'][$i]]=$_POST['buy'][$i];
$_session[$_POST['kd_unor'][$i]]=$_POST['kd_unor'][$i];
$total[i]=$_session[$_POST['kd_unor'][$i]]*$_session[$_POST['buy'][$i]];
if($total[i]!==0) {
$totalSum +=$total[i];
echo "<tr><td>".$_session[$i]."</td><td>" .$_session[$_POST['buy'][$i]]." X </td><td>".$_session[$_POST['kd_unor'][$i]]."=</td><td>".$total[i]."</td><td></tr>";
}
}
echo "<tr><td colspan='3' align='right'><b>TOTAL</b></td><td>".$totalSum."</td><td></tr>";
echo"</table>";
?>