2014-03-31 30 views
0

如何自动总结tfoot中的“​​总时数”数据并保存到MySQL数据库并在同一区域重新显示?我有两个例子工作,我想添加自动求和功能的VizaHours网络应用程序。如何自动求和列并保存到MySQL数据库?

工作的例子在这里:

EDITED 14年4月2日下面加所有代码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 &gt;&gt;&gt;&gt;&gt;</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> 

EDITED 14年4月2日下面加所有代码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"); 

?> 

EDITED 14年4月2日下面加所有代码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> 
+1

我只能看到一个HTML表。你的PHP逻辑在哪里? – Raptor

+0

谢谢Raptor ... new to posting here ...现在所有的代码,但MySQL connect.php页面。 :) – Bigfootbud

回答

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> 
+0

使用好的逻辑! – sunny

+0

感谢您的回复! :)这里是除了实际的数据库连接PHP之外的所有代码...不要以为你需要...? – Bigfootbud

相关问题