PHP表中的Sum列

时间:2015-01-13 14:31:32

标签: php html mysql html-table sum

我有一个带有各种SQL查询的index.php,可以帮助我查找各个帐户的余额。这是一个试算平衡,所以我需要汇总借方栏中的所有金额和SUM信用列中的所有金额,以便我可以统计这两个金额。 SUM请帮帮我。所有值都回显到PHP表。

index.php

<?php
    echo "<table border='1'>";
    echo "<tr><th width='150'>Account</th><th width='200'>Debit</th><th width='200'>Credit</th></tr>";
    echo "<tr><th align='left'>Cash & Bank</th></tr>";


    //List the Banks first with their respective balances.
    $bank_sql = "SELECT * FROM bank";
    $bank_query = mysqli_query($conn, $bank_sql);
    while ($bank_result = mysqli_fetch_array($bank_query)){
    $bank_name = $bank_result['name'];

    $sql= "SELECT SUM(amount) FROM account WHERE (mode='$bank_name' AND status='completed') AND (type='p' OR type='r')";
    $sql_query = mysqli_query($conn, $sql);
    while($sql_result = mysqli_fetch_array($sql_query)){
        $sql_value = $sql_result['SUM(amount)'];
    }

    $sql1 = "SELECT SUM(amount) FROM account WHERE (mode='$bank_name' AND status='completed') AND (type='s' OR type='pa')";
    $sql1_query = mysqli_query($conn, $sql1);
    while($sql1_result = mysqli_fetch_array($sql1_query)){
        $sql1_value = $sql1_result['SUM(amount)'];
    }

    echo "<tr><td>".$bank_name."</td>";
    if ($sql_value > $sql1_value){
        echo "<td align='right'>AED " . number_format(($sql_value - $sql1_value),2) . "</td><td>&nbsp</td>";
    }
        elseif ($sql1_value > $sql_value) {
        echo "<td>&nbsp</td><td align='right'>AED " . number_format(($sql1_value - $sql_value),2) . "</td>";
        }
        else {
            echo "<td>&nbsp</td><td>&nbsp</td>";
        }
    }
    echo "</tr></table>";
    }
    ?>

2 个答案:

答案 0 :(得分:0)

Try the below query
SELECT SUM(amount) FROM ht_account WHERE mode='$bank_name' AND status='completed' AND type IN('sale','purchase','reciept','payment') 

答案 1 :(得分:0)

快速做到这一点就是。

$sql3="select column1,column2 from yourtable where ..condition";
$sumofcolumn1;
$sumofcolumn2;
    while($sql3_result = mysqli_fetch_array($sql3))
     {
       $sumofcolumn2+=$sql3_result['column2'];
       $sumofcolumn1+=$sql3_result['column1'];
     }
echo $sumofcolumn1.' '.$sumofcolumn2