如何计算php中的数据总和?

时间:2017-07-09 10:16:38

标签: php html-table sum

enter image description here

在这张表中我有多个值,如CGST,SGST,NET AMOUNT,MRP我想在下面给出CGST,SGCST等所有值的总和......

请帮我查看如何查找所有数据的总数。

这是代码:

<table class="table table-striped">
                        <thead>
                            <tr>
                                <th><strong>Sr. No</strong></th>
                                <th><strong>Item Name</strong></th>
                                <th><strong>Serial No</strong></th>
                                <th><strong>CGST</strong></th>
                                <th><strong>SGST</strong></th>
                                <th><strong>NET AMOUNT</strong></th>
                                <th><strong>MRP</strong></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                                $counter = 0;

                                    $getData = mysql_query("SELECT CUSTOMER_ID, ITEM_ID, DATE, FLAG, GROUP_CONCAT(SERIAL_NO) AS SERIAL_NO FROM cart_mst WHERE CUSTOMER_ID = '".$_SESSION['cust']."' AND FLAG = '0' GROUP BY ITEM_ID");

                                    while($row = mysql_fetch_array($getData))
                                    {
                                        $getItem = mysql_query("SELECT * FROM item_details WHERE ITEM_ID = '".$row['ITEM_ID']."'");
                                        $rowItem = mysql_fetch_array($getItem);

                            ?>  

                            <tr>
                                <td><?php echo ++$counter ?></td>
                                <td>
                                    <?php echo $rowItem['ITEM_NAME'];

                                        $schools_array = explode(",", $row['SERIAL_NO']);
                                        $qty = count($schools_array); 
                                    ?>
                                </td>
                                <td>
                                    <?php 
                                        $hobbies = explode(',', $row['SERIAL_NO']);
                                        foreach ($hobbies as $hobby) 
                                        {
                                            // output each hobby and decorate/separate them however you'd like
                                            echo $hobby . ', ';
                                        }
                                    ?>
                                </td>

                                <td>
                                    <?php
                                        if($rowItem['TYPE'] == "BAT")
                                        {
                                            $mrp = $rowItem['MRP'];
                                            $tot = $mrp * $qty;
                                            $net_amt = $tot * 100/128;
                                            $cgst = $net_amt * 14/100;
                                            echo round($cgst, 2);
                                        }
                                        else
                                        {
                                            $mrp = $rowItem['MRP'];
                                            $tot = $mrp * $qty;
                                            $net_amt = $tot * 100/118;
                                            $cgst = $net_amt * 9/100;
                                            echo round($cgst, 2);
                                        }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                        if($rowItem['TYPE'] == "BAT")
                                        {
                                            $mrp = $rowItem['MRP'];
                                            $tot = $mrp * $qty;
                                            $net_amt = $tot * 100/128;
                                            $sgst = $net_amt * 14/100;
                                            echo round($cgst, 2);
                                        }
                                        else
                                        {
                                            $mrp = $rowItem['MRP'];
                                            $tot = $mrp * $qty;
                                            $net_amt = $tot * 100/118;
                                            $cgst = $net_amt * 9/100;
                                            echo round($cgst, 2);
                                        }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                        if($rowItem['TYPE'] =="BAT")
                                        {
                                            $mrp = $rowItem['MRP'];
                                            $tot = $mrp * $qty;
                                            $net_amt = $tot * 100/128;
                                            echo round($net_amt, 2);
                                        }
                                        else
                                        {
                                            $mrp = $rowItem['MRP'];
                                            $tot = $mrp * $qty;
                                            $net_amt = $tot * 100/118;
                                            echo round($net_amt, 2);
                                        }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                        $a = $rowItem['MRP'];
                                        $tot = $a * $qty;
                                        echo $tot;
                                    ?>
                                </td>
                            </tr>

                            <?php
                                    }

                            ?>
                        </tbody>
                    </table>

2 个答案:

答案 0 :(得分:1)

This is simple first you initalize your variable as 0.
$cgst=0;
$sgst=0;
$netAmount=0;
$totalAmount=0;
In loop
$cgst=$cgst+Your Cgst Amount of current row; 
$sgst=$sgst+Your Sgst Amount of current row;
$netAmount=$netAmount+Your Net Amount of current row;
$totalAmount=$totalAmount+Your Total Amount of current row;

答案 1 :(得分:1)

你可以这样做:当你从循环迭代时,对每个变量求和。循环后,您将找到总值

<table class="table table-striped">
    <thead>
        <tr>
            <th><strong>Sr. No</strong></th>
            <th><strong>Item Name</strong></th>
            <th><strong>Serial No</strong></th>
            <th><strong>CGST</strong></th>
            <th><strong>SGST</strong></th>
            <th><strong>NET AMOUNT</strong></th>
            <th><strong>MRP</strong></th>
        </tr>
    </thead>
    <tbody>
        <?php
            $counter = 0;

                $getData = mysql_query("SELECT CUSTOMER_ID, ITEM_ID, DATE, FLAG, GROUP_CONCAT(SERIAL_NO) AS SERIAL_NO FROM cart_mst WHERE CUSTOMER_ID = '".$_SESSION['cust']."' AND FLAG = '0' GROUP BY ITEM_ID");
                $cgst_sum = 0;
                $sgst_sum = 0;
                $net_sum = 0;
                $mrp_sum = 0;
                while($row = mysql_fetch_array($getData))
                {
                    $getItem = mysql_query("SELECT * FROM item_details WHERE ITEM_ID = '".$row['ITEM_ID']."'");
                    $rowItem = mysql_fetch_array($getItem);

        ?>  

        <tr>
            <td><?php echo ++$counter ?></td>
            <td>
                <?php echo $rowItem['ITEM_NAME'];

                    $schools_array = explode(",", $row['SERIAL_NO']);
                    $qty = count($schools_array); 
                ?>
            </td>
            <td>
                <?php 
                    $hobbies = explode(',', $row['SERIAL_NO']);
                    foreach ($hobbies as $hobby) 
                    {
                        // output each hobby and decorate/separate them however you'd like
                        echo $hobby . ', ';
                    }
                ?>
            </td>

            <td>
                <?php
                    if($rowItem['TYPE'] == "BAT")
                    {
                        $mrp = $rowItem['MRP'];
                        $tot = $mrp * $qty;
                        $net_amt = $tot * 100/128;
                        $cgst = $net_amt * 14/100;
                        echo round($cgst, 2);
                    }
                    else
                    {
                        $mrp = $rowItem['MRP'];
                        $tot = $mrp * $qty;
                        $net_amt = $tot * 100/118;
                        $cgst = $net_amt * 9/100;
                        echo round($cgst, 2);
                    }
                    $cgst_sum += $cgst;
                ?>
            </td>
            <td>
                <?php
                    if($rowItem['TYPE'] == "BAT")
                    {
                        $mrp = $rowItem['MRP'];
                        $tot = $mrp * $qty;
                        $net_amt = $tot * 100/128;
                        $sgst = $net_amt * 14/100;
                        echo round($cgst, 2);
                    }
                    else
                    {
                        $mrp = $rowItem['MRP'];
                        $tot = $mrp * $qty;
                        $net_amt = $tot * 100/118;
                        $sgst = $net_amt * 9/100;
                        echo round($sgst, 2);
                    }
                    $sgst_sum += $sgst;
                ?>
            </td>
            <td>
                <?php
                    if($rowItem['TYPE'] =="BAT")
                    {
                        $mrp = $rowItem['MRP'];
                        $tot = $mrp * $qty;
                        $net_amt = $tot * 100/128;
                        echo round($net_amt, 2);
                    }
                    else
                    {
                        $mrp = $rowItem['MRP'];
                        $tot = $mrp * $qty;
                        $net_amt = $tot * 100/118;
                        echo round($net_amt, 2);
                    }
                    $net_sum += $net_amt;
                ?>
            </td>
            <td>
                <?php
                    $a = $rowItem['MRP'];
                    $tot = $a * $qty;
                    echo $tot;
                    $mrp_sum += $tot;
                ?>
            </td>
        </tr>

        <?php
                }

        ?>
    </tbody>
</table>
<?php
echo "Sum of cgst =".$cgst_sum."<br>";
echo "Sum of sgst =".$sgst_sum."<br>";
echo "Sum of Net amount =".$net_sum."<br>";
echo "Sum of mrp =".$mrp_sum."<br>";
?>