【问题标题】:How to Calculate sum of data in php?如何计算php中的数据总和?
【发布时间】:2017-07-09 10:16:38
【问题描述】:

在此表中,我有多个值,例如 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>

【问题讨论】:

    标签: php html-table sum


    【解决方案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>";
    ?>
    

    【讨论】:

      【解决方案2】:
      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;
      

      【讨论】:

        猜你喜欢
        • 2015-06-16
        • 1970-01-01
        • 2011-08-19
        • 2020-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多