您可以使用jQuery或在循環的變量。 環PHP總結
$sum = 0;
while($row2= mysql_fetch_array($data2))
{
echo "<td align='right'><input type='text' name='txt[]' value='".$row2['Price']."'/></td>";
$sum += $row2['Price'];
}
echo $sum;
jQuery的版本
<div class="section">
while($row2= mysql_fetch_array($data2))
{
echo "<td align='right'><input type='text' name='txt[]' value='".$row2['Price']."'/></td>";
}
<div id="mysum"></div>
</div>
<script>
$('.section').each(function(){
var totalPoints = 0;
$(this).find('input').each(function(i,n){
totalPoints += parseInt($(n).val(),10);
});
alert(totalPoints);
$('#mysum').html(totalPoints);
});
</script>
PDO版本,請從棄用mysql_fetch等例子變換你的腳本
try {
$name= mysql_real_escape_string($row['Name']);
$db = new PDO('mysql:host=localhost;dbname=tablename', 'user', 'password');
$stmt = $db->prepare('SELECT * FROM tblstockmgt WHERE ItemName:=this');
$stmt->bindValue(':this', $name, PDO::PARAM_STR);
$stmt->execute();
$results = $stmt->fetchAll();
$sum = 0;
if ($stmt->rowCount() == 0) {
echo "No data";
die();
}
foreach ($results as $result) {
echo "<td align='right'><input type='text' name='txt[]' value='".$result['Price']."'/></td>";
$sum += $result['Price'];
}
echo $sum;
}
catch (PDOException $ex) {
die($ex->getMessage());
}
在哪些文字總計會顯示? – 2014-11-04 11:37:36