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



我目前正在编写一个会计代码,该代码将根据MySql中存储的数据打印试算表。我似乎无法理解应该正确显示试算表的数组。数据集看起来有点像这样:

id | 账户 nbsp;| nbsp nbsp;金额 | nbsp nbsp;付费模式 
1 nbsp;TUITION nbsp nbsp nbsp nbsp;5000 nbsp nbsp nbsp nbsp nbsp;支票
2 nbsp TUITION nbsp nbsp nbsp nbsp;2000 nbsp nbsp nbsp nbsp nbsp nbsp;现金 
3 nbsp 维修 nbsp nbsp nbsp nbsp nbsp;500 nbsp nbsp nbsp nbsp nbsp nbsp;现金 

试算表的输出应该以如下的复式输入格式显示:

账户 nbsp;| nbsp nbsp;债务 | nbsp nbsp;信贷 
银行 nbsp nbsp nbsp nbsp nbsp;5000
现金 nbsp nbsp nbsp nbsp nbsp;2000 nbsp nbsp nbsp 500
TUITION nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp;7000 
维修 nbsp nbsp nbsp nbsp nbsp;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>';

编辑:以上语句的当前输出:

账户 nbsp;| nbsp nbsp;债务 | nbsp nbsp;信贷 
TUITION nbsp nbsp nbsp nbsp;7000 
维修 nbsp nbsp nbsp nbsp nbsp;500 

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

提前感谢

好的(我应该在那些账户讲座中更加专注)。现在我不知道一个账户(银行和现金除外)是否可以同时有借记和贷记,但由于你的示例条目没有显示,我猜没有。此外,下面的代码假设(如上面的注释所示)您有一列(我将其命名为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

相关内容

  • 没有找到相关文章

最新更新