PHP / MySQL多维/关联数组3试算平衡

时间:2014-04-20 10:30:56

标签: php mysql multidimensional-array associative-array

我目前正在研究一种会计代码,该代码将根据存储在MySql中的数据打印出试算表。我似乎无法理解应该正确显示试算平衡的阵列。数据集看起来像这样:

id |帐户|金额| paymode
1 TUITION 5000检查
2学期2000现金
3修理500现金

试算表的输出应以双输入格式显示,如下所示:

帐户| DEBIT |信用证 银行5000
现金2000 500
TUITION 7000
修理500

请注意,上面必须根据付费模式字段自动生成两个帐户(银行和现金)......它们的值(本例中为5000和2000)也取决于“金额“栏目。

所以到目前为止我的当前代码看起来像这样:(它输出帐户总数但仅在DEBIT方面)

// Make the query(Get Accounts and Totals):
$rein = @mysqli_query ($con, "SELECT account, amount, paymode FROM tbl_finance"); // Run the query.

$bg = '#eeeeee';
$accarr = array();
while($rowin = mysqli_fetch_array($rein, MYSQLI_ASSOC)) {
    if(!isset($accarr[$rowin['type']])) {
    $accarr[$rowin['type']]['amount'] = 0;
}
$sum = $rowin['amount_paid'];
$accarr[$rowin['type']]['amount'] += $sum;
$accarr[$rowin['type']]['paymode'] = $rowin['payterm'];
} // End While
mysqli_free_result($rein);
print_r($accarr);

echo '
<table>
<tr>
    <td align="left"><b>ACCOUNT</b></td>
    <td align="right"><b>DEBIT</b></td>
    <td align="right"><b>CREDIT</b></td>
</tr>
';

$bg = '#eeeeee';
foreach($accarr as $acc => $data) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

echo '
<tr bgcolor="'. $bg .'">
    <td align="left">'. $acc .'</td>
        <td align="right">'. $data['amount'] .'</td>
</tr>
';
} // End foreach

echo '</table>';

编辑:上述声明的当前输出:

帐户| DEBIT |信用证 TUITION 7000
修理500

问题:上面的数据集如何用于输出上面显示的数据(在适当情况下与DEBIT和CREDIT对齐)?

先谢谢

1 个答案:

答案 0 :(得分:0)

好的(在这些帐户讲座中我应该更加集中注意力)。现在我不知道一个帐户(银行和现金除外)是否可以同时借记和贷记,但由于您的示例条目没有表明,我猜它没有。此外,下面的代码假定(如上面的注释中所示)您有一个列(我将其命名为transaction),表示它是Dr还是Cr条目。

//this array will hold bank and cash values
$main_acc=array('bank'=>array('Dr'=>0,'Cr'=>0), 'cash'=>array('Dr'=>0,'Cr'=>0));

//this one to hold other accounts and their amounts, transaction etc.
$acc = array();
while($rowin = mysqli_fetch_array($rein, MYSQLI_ASSOC)) {
    /* store bank transaction type and amount */
    if($rowin['paymode']=='Cheque'){
        $main_acc['bank'][$rowin['transaction']] =$main_acc['bank'][$rowin['transaction']] + $rowin['amount'];
    }
    /* store cash transaction type and amount */
    if($rowin['paymode']=='Cash'){
        $main_acc['cash'][$rowin['transaction']] = $main_acc['cash'][$rowin['transaction']] + $rowin['amount'];
    }
    /* store other account transaction type and increment
       amount if already exists */
    if(isset($acc[$rowin['account']])){
        $acc[$rowin['account']]['amount'] = $acc[$rowin['account']]['amount'] +$rowin['amount'];
    }
    else
    {
        $acc[$rowin['account']]['trans'] = $rowin['transaction'];
        $acc[$rowin['account']]['amount'] = $rowin['amount'];

    }

}

echo '
<table>
<tr>
    <td align="left"><b>ACCOUNT</b></td>
    <td align="right"><b>DEBIT</b></td>
    <td align="right"><b>CREDIT</b></td>
</tr>
';

$bg = '#eeeeee';
//displaying bank and cash
foreach($main_acc as $key=>$val){

            $bg = ($bg=='#eeeeee') ? '#ffffff' : '#eeeeee';
            echo '<tr bgcolor="'. $bg .'">
                    <td align="left">'. $key .'</td>
                    <td align="left">'. (($val['Cr']!=0)?$val['Cr']:'') .'</td>
                    <td align="right">'. (($val['Dr']!=0)?$val['Dr']:'').'</td>
                  </tr>
        ';
}

//displaying other accounts
foreach($acc as $key=>$val){

            $bg = ($bg=='#eeeeee') ? '#ffffff' : '#eeeeee';
            echo '<tr bgcolor="'. $bg .'">
                    <td align="left">'. $key .'</td>
                    <td align="left">'. (($val['trans']=='Dr')?$val['amount']:'') .'</td>
                    <td align="right">'. (($val['trans']=='Cr')?$val['amount']:'') .'</td>
                  </tr>
        ';
}
echo '</table>';

输出:

ACCOUNT DEBIT   CREDIT
bank    5000    
cash    2000      500
TUITION          7000
Repairs 500