Question

I'm currently working on an accounting code that will print out a Trial Balance based on data stored in MySql. I can't seem to get my head around the arrays that should get the trial balance displayed correctly. The data-set looks somewhat like this:

id |     account      |        amount     |        paymode    
1       TUITION               5000                 Cheque  
2       TUITION               2000                 Cash   
3       Repairs                  500                 Cash  

The output of the trial balance should display in double entry format like this:

ACCOUNT      |        DEBIT     |        CREDIT    
Bank                       5000
Cash                       2000                 500
TUITION                                         7000    
Repairs                     500    

Please note above that two accounts(Bank and Cash) have to be generated automatically by code depending on the paymode field...and their values(5000 and 2000 in this case) are also dependent on the values found under the "amount" column.

So my current code looks like this so far: (It outputs accounts in their total but only on the DEBIT side)

// 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>';

Edit: The current output of above statement:

ACCOUNT      |        DEBIT     |        CREDIT    
TUITION                   7000    
Repairs                     500    

Question: How can the data-set above be used to output data as displayed above(aligned to DEBIT and CREDIT where appropriate)?

Thanks in Advance

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top