我目前正在处理一个会计代码,该代码将根据MySql中存储的数据打印出“试用余额”。我似乎无法绕开应该正确显示试算表的阵列。数据集看起来像这样:
id |帐户|金额|支付方式
1学费5000支票
2学费2000现金
3修理500现金
试算表的输出应以双重输入格式显示,如下所示:
帐户|借方|信用
银行5000
现金2000现金500
学费学费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>';
编辑:以上语句的当前输出:
帐户|借方|信用
学费7000
修理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
关于php - PHP/MySQL多维/关联数组3试用平衡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23181047/