如果在for循环
中重复相同的id,我试图对这些值求和<?php
$tax = count($_POST['tax_id']);
for($i=0;$i<$tax;$i++) {
$tax_id = $_POST['tax_id'][$i];
$tax_amount = $_POST['amount'][$i];
$sql = "SELECT * FROM `ca_taxmaster` where tax_comp = $tax_id ";
$now = mysql_query($sql) or die(mysql_error());
while($row1 = mysql_fetch_array($now)){
$per = $row1['tax_Percent'];
$tax_calc = ($tax_amount*$per)/100;
$sum_total += $tax_calc;
?>
<tr>
<td class="text-right" colspan="5"><b><?php echo $row1['tax_type']; ?></b></td>
<td class="text-right"></td>
<td class="text-right"><?php echo $tax_calc;?></td>
</tr>
<?php } } ?>
我的输出就像 在Gst的税收栏中我有sgst&amp; cgst如果我选择第二次我需要sgst&amp; cgst的总和
No. Job Description SAC Code Tax Amount
0 teaching desc1 sa001 GST - 1000
1 engineer desc2 sac002 IGST-Master - 2000
2 doctor desc3 sac003 GST - 1000
Sub-Total Rs.4000
SGST 90
CGST 90
IGST 360
SGST 90
CGST 90
Total Rs.4720
但我需要得到如果相同的id重复它应该总和值
SGST 180
CGST 180
IGST 360
Total Rs.4720
和我的sql结构如下: sql table structure
如果还有其他办法,请帮帮我。谢谢提前
答案 0 :(得分:0)
根据您共享的表格结构,您可以使用此查询替换您的查询:
SELECT sum(tax_Percent)
FROM ca_taxmaster where tax_comp = $tax_id
GROUP BY tax_type;
答案 1 :(得分:0)
你可以试试这个:
<?php
$tax = count($_POST['tax_id']);
$sum_total = 0;
for($i=0;$i<$tax;$i++) {
$tax_id = $_POST['tax_id'][$i];
$tax_amount = $_POST['amount'][$i];
$sql = "SELECT * FROM `ca_taxmaster` where tax_comp = $tax_id ";
$now = mysql_query($sql) or die(mysql_error());
while($row1 = mysql_fetch_array($now)){
$per = $row1['tax_Percent'];
$tax_calc = ($tax_amount*$per)/100;
$sum_total = $sum_total + $tax_calc;
?>
<tr>
<td class="text-right" colspan="5"><b><?php echo $row1['tax_type']; ?></b></td>
<td class="text-right"></td>
<td class="text-right"><?php echo $tax_calc;?></td>
</tr>
<?php } } ?>