2016-08-03 56 views
0

我有一个从数据库填充的动态表。我的代码如下:如何求和从数据库填充的值

<table class="table table-bordered" id="tb"> 
    <thead> 
    <tr style="BACKGROUND-COLOR: DarkGrey "> 
     <th colspan="5" style="text-align:Center;">Objectives</th> 
    </tr> 
    <tr class="tr-header"> 
     <th style="text-align:Center;width:10%;">ID</th> 
     <th colspan="2">Targets</th> 
     <th style="text-align:Center;width:1%;" rowspan="2">Weightage %</th> 
    </tr> 
    <tr> 
     <th> </th> 
     <th> </th> 
     <th style="width:10%;"> Date </th> 
    </tr> 
    </thead> 
    <tbody 
    <?PHP 
    $myquery = "select subtotals,Serial_Number,Targets,Weightage, row_number,edit_details 
       from Table1 WHERE Serial_Number='$Serial_Number' ORDER BY Row_Number asc"; 
    $fetched = sqlsrv_query($conn, $myquery); 
    if ($fetched === false) { 
    die(print_r(sqlsrv_errors(), true)); 
    } 
    while ($res = sqlsrv_fetch_array($fetched, SQLSRV_FETCH_ASSOC)) { 
    $Weightage = $res['Weightage']; 
    $Targets = $res['Targets']; 
    $Row_Number = $res['row_number']; 
    $subtotals = $res['subtotals']; 
    ?> 
    <tr id="<?php //echo $id ?>" class="tredit"> 
     <td class="edittbl"> 
     <b><input type="text" name="ID[]" class="ASDF form-control" id="Row<?php echo $Row_Number ?>" value="<?php echo $Row_Number; ?>" readonly></input></b> 
     </td>            
     <?PHP 
     if ($Objectives == "SubTotal") { 
     ?> 
     <td class="edittbl" colspan="3"> 
      <b><input type="text" name="Subtotal[]" class=" ASDF form-control" value="<?php echo $Objectives; ?>" style="text-align:center;" readonly ></input></b> 
     </td> 
     <td class="edittbl"> 
      <b><input type="text" name="SubTotal_Weightage[]" class="ASDF form-control" value="<?php echo $Weightage; ?>" readonly></input></b> 
     </td>            
     <?PHP 
     } else { 
     ?> 
     <td class="edittbl"> 
      <input type="text" name="Targets[]" class="ASDF form-control" value="<?php echo $Targets; ?>" ></input> 
     </td> 
     <td class="edittbl"> 
      <input type="text" name="Weightage[]" class="ASDF form-control" value="<?php echo $Weightage; ?>"></input> 
     </td> 
     <?PHP } ?> 
     </tr>     
    <?PHP } ?> 
    </tbody> 
</table> 

我试图做小计一自动计算,当值发生改变,这样当的权重发生变化时,小计自动计算并给出了新的价值。

Calculation Image

这些weightages是动态的,并且小计的DONOT具有固定的位置。我如何确保如果前四项有任何变化,第一小计会发生变化,如果第二项和第三项有变化,它会改变第二项小计?

希望有任何建议。

+1

非常重要的一点。没有两个输入可以在同一页*中具有相同的ID *。核实。 'id =“Row <?php echo $ Row_Number?>”'每个输入都有相同的id。 –

回答

0

首先,为什么不从视图中分离控制器逻辑和模型?它会让你的代码更清洁。 根据您粘贴的代码片段,我假设权重会根据用户通过表单字段的输入而改变。我真的不明白你如何计算小计,但我认为你应该为此实现一个AJAX样式的解决方案,以便当权重改变时,请求被发送到模型重新计算小计,要么通过从数据库中获取或来自某种形式的逻辑。