Ok (I should've paid more concentration during those accounts lectures). Now I don't know if an account (except Bank and Cash) can have debit and credit at the same time, but since your example entries didn't indicate, I'm guessing it doesn't. Also, the below code assumes (like indicated in the comment above) that you have a column (I named it transaction
) which says whether it's a Dr
or Cr
entry.
//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>';
Output:
ACCOUNT DEBIT CREDIT
bank 5000
cash 2000 500
TUITION 7000
Repairs 500