0

I was trying to sum the values if the same id repeat in for loop

<?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 } } ?>

my output like in tax colum for Gst I has sgst&cgst if I select second time I need the sum of sgst&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

but I need to get If same id repeating it should sum the values

                                                    SGST        180
                                                    CGST        180
                                                    IGST        360
                                                   Total        Rs.4720

and my sql structure like: sql table structure

Please help me if it has anyother way to do.Thanks in advance

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
Jennifer
  • 1
  • 2
  • Why is this question tagged with Codeigniter? – jtheman Mar 23 '18 at 11:23
  • Please, [don't use `mysql_*` functions](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – jtheman Mar 23 '18 at 11:27
  • 1
    Maybe `select sum(amount), Sub-Total from table group by Sub-Total`, I also wouldn't use hyphens in column names if that is the real name. – chris85 Mar 23 '18 at 11:42

2 Answers2

0

As per the table structure shared by you, you can use replace your query with this query:

SELECT sum(tax_Percent) 
FROM ca_taxmaster where tax_comp = $tax_id 
GROUP BY tax_type;
Syscall
  • 19,327
  • 10
  • 37
  • 52
Pooja Rani
  • 11
  • 2
0

You can try this :

<?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 } } ?>