我在计算数据库和用户输入的数据总和时遇到了问题,你们能帮我吗?
这是php代码
第一个值(stok di database)是来自数据库的数据,第二个值(stok fisik)是来自用户输入的数据,selishih是第二个值-第一个值。
<div class="box-content nopadding" style="overflow: auto;">
<table width="400px" class="table table-hover table-nomargin table-bordered">
<thead>
<tr>
<th style="text-align: center; vertical-align:middle;" rowspan="2">No</th>
<th style="text-align: center; vertical-align:middle;" rowspan="2">Nama Barang</th>
<th style="text-align: center; vertical-align:middle;" colspan="2">Stok</th>
<th style="text-align: center; vertical-align:middle;" rowspan="2">Selisih</th>
</tr>
<tr>
<th style="text-align: center; vertical-align:middle;">Stok di Database</th>
<th style="text-align: center; vertical-align:middle;">Stok Fisik</th>
</tr>
</thead>
<tbody>
<?php
$data = $db->query("select id, nama_barang, stock from tbl_atk group by nama_barang");
for ($i = 0; $i < count($data); $i++) {
$no = $i + 1;
if ($no % 2 ==0) $bg ='#FBFBFB';
else $bg = '#FFFFFF';
// $total = 'result' - $data[$i]['stock'];
$tot_masuk = $tot_masuk + "stock_op";
$tot_keluar = $tot_keluar + $data[$i]['stock'];
$grand_tot = $tot_masuk - $tot_keluar;
?>
<tr>
<td style="text-align: center; vertical-align:middle;"><?php echo $no; ?></td>
<td><?php echo $data[$i]['nama_barang']?></td>
<td style="text-align: right;" class="so"><?php echo $data[$i]['stock']; ?></td>
<td><input type="number" class="form-control so" id="stock_op" name="stock_op" style="text-align: left"></td>
<td style="text-align: right; font-weight:bold;"><output class="result"></output></td>
</tr>
<?php
}
?>
<script src="../js/jquery.min.js"></script>
<script>
$(document).on('input','.so',function(){
var totalSum = 0;
var currentRow = $(this).closest('tr');
currentRow.find('.so').each(function(){
var inputVal = ($(this).is('input')) ? $(this).val() : parseInt($(this).html());
console.log(inputVal);
if($.isNumeric(inputVal)){
totalSum += parseFloat(inputVal);
}
});
currentRow.find('.result').val(totalSum);
});
</script>
<tr style="background-color:#eaeaea; font-weight:bold;">
<td style="text-align: center; vertical-align:middle;"></td>
<td style="text-align: right;">Selisih</td>
<td style="text-align: right;"><?php echo $tot_keluar; ?></td>
<td style="text-align: right;"><?php echo $tot_masuk; ?></td>
<td style="text-align: right;"><?php echo $grand_tot; ?></td>
</tr>
</tbody>
</table>
</div>
这是用户界面
2条答案
按热度按时间o75abkj41#
更改
.so
至so
在下面的输入字段中。将id结果更改为下行中的类结果
新的更新
给你的stok-fisik单元指定一个类
将jquery代码更改为
6psbrbz92#
你好,fauzio,我创建了一个包含3列的示例表,
1t\u id(即id)
2姓名(即姓名barang)
三。t\U值(即库存di数据库)
我创建了一个表并显示了表中每一行的值。列t\u value中的所有值都添加到java脚本数组中。然后,当用户输入特定行的值时。
单击按钮后,该特定行的值将从db\u列和user列中选取,结果将打印在result列中