如何自动汇总tfoot中的“Total Hours”数据并保存到MySQL数据库并在同一区域重新显示?我有两个工作示例,我想在VizaHours网络应用程序中添加自动求和功能。
工作示例如下:
已编辑4-2-14在下方添加所有代码index.php
:
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<table>
<thead>
<tr>
<th> DAY </th>
<th> DAY INFO </th>
<th> HOURS </th>
</tr>
<tfoot>
<tr id="summation">
<td class="total-hours" colspan="2">TOTAL HRS >>>>></td>
<td><span>0</span></td>
</tr>
<tr>
<th colspan="3">BUD HOURS - START WEEK 3-24-14<br />unpaid</th>
</tr>
</tfoot>
</thead>
<tbody>
<?php
include('connect.php');
$result = $db->prepare("SELECT * FROM budsvizahours ORDER BY id DESC");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['fname']; ?><br /><br /><a href="editform.php?id=<?php echo
$row['id']; ?>"> Edit </a></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['age']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
已编辑4-2-14在下方添加所有代码edit.php
:
<?php
// configuration
include('connect.php');
// new data
$lname = $_POST['lname'];
$fname = $_POST['fname'];
$age = $_POST['age'];
$id = $_POST['memids'];
// query
$sql = "UPDATE budsvizahours
SET fname=?, lname=?, age=?
WHERE id=?";
$q = $db->prepare($sql);
$q->execute(array($fname,$lname,$age,$id));
header("location: index.php");
?>
已编辑4-2-14在下方添加所有代码editform.php
:
<?php
include('connect.php');
$id=$_GET['id'];
$result = $db->prepare("SELECT * FROM budsvizahours WHERE id= :userid");
$result->bindParam(':userid', $id);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<form action="edit.php" method="POST">
<input type="hidden" name="memids" value="<?php echo $id; ?>" />
<br>
<input type="hidden" name="fname" value="<?php echo $row['fname']; ?>" /><br>
Update Day Info<br>
<input type="text" name="lname" value="<?php echo $row['lname']; ?>" /><br>
Hours This Day<br>
<input class="record" type="tel" name="age" value="<?php echo $row['age']; ?>" />
<br><br>
<button class="edit-info-button" type="submit" value="Save">Save</button>
</form>
<?php
}
?>
<script>
$(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".txt").each(function() {
$(this).keyup(function(){
calculateSum();
});
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value); }
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").html(sum.toFixed(2));
}
</script>
</body>
</html>
答案 0 :(得分:1)
//it work for your code...
//sum query is good but i case you don't want to do it by that way
<script type="text/javascript">
$(document).ready(function() {
var sum = cls_nm_tr = 0;//create variable to null or zero
//making loop for your tr its depends on no of tr you have
$('.record').each(function(i,e){
var cls_nm_tr = $('td:eq(2)', this).html();//getting value from td
sum = parseInt(sum) + parseInt(cls_nm_tr);//adding them in sum varible
});//loops ends here
console.log(sum);//here you got sum of HOURS.//"sum" variable have sum or hours
//$(".yoru_footer_id").html(sum);//here you can put data in tfooter
});
</script>