I have a function that pulls data from mysql as follows;
function displayTrialBalance(){
require_once('Connections/mpcs_dbConn.php');
mysql_select_db($database_dbConn, $dbConn);
// declare query variables
$query_diff_empty = "SELECT Account_Code, sum(Amount) AS difference FROM journal2 GROUP BY Account_Code ORDER BY difference asc, Account_Code asc";
// check if user wants empty
// query DB
$amounts = mysql_query($query_diff_empty, $dbConn) or die(mysql_error());
$row_amounts = mysql_fetch_array($amounts);
$totalRows_users = mysql_num_rows($amounts);
if ($row_amounts){
function debit($arr){
return ($arr < 0);
}
function credit($arr){
return ($arr >= 0);
}
echo '<table align="center" border="0" bordercolor="#666666" cellpadding="5" cellspacing="3">
<tr class="tableTop">
<td>Account Code</td>
<td>Debit</td>
<td>Credit</td>';
do {
$diff = array($row_amounts['difference']);
echo '
</tr>
<tr class="tableButtom">
<td>'.$row_amounts['Account_Code'].'</td>
<td>';
foreach((array_filter($diff, "debit")) as $new){
echo abs($new).'.00';}
echo '</td>
<td>';
foreach((array_filter($diff, "credit")) as $new2){
echo $new2;
}
echo '</td>';
}while ($row_amounts = mysql_fetch_array($amounts));
echo '</tr>
</table>';
}
}
When i call displayTrialBalance() i get:
Account Code Debit Credit
13101 350000.00
22601 3000.00
22201 0.00
41101 3000.00
21201 40000.00
How can i sum the negative(Debit column) and positive(Credit column) numbers to get a total at the end the output table?
Also is it possible to sort the Account Code column in ascending order so that my expected output should be:
Account Code Debit Credit
13101 350000.00
22601 3000.00
21201 40000.00
22201 0.00
41101 3000.00
-----------------------------------
Total 353000.00 43000.00
I figured it out eventually, all i needed to do was to loop the array and collect the increments in another variable. Then echo the variable later in my table as the total.
function displayTrialBalance(){
require_once('Connections/mpcs_dbConn.php');
mysql_select_db($database_dbConn, $dbConn);
// declare query variables
$query_diff_empty = "SELECT Account_Code, sum(Amount) AS difference FROM journal2 GROUP BY Account_Code ORDER BY difference asc, Account_Code asc";
// check if user wants empty
// query DB
$amounts = mysql_query($query_diff_empty, $dbConn) or die(mysql_error());
$row_amounts = mysql_fetch_array($amounts);
$totalRows_users = mysql_num_rows($amounts);
if ($row_amounts){
function debit($arr){
return ($arr < 0);
}
function credit($arr){
return ($arr >= 0);
}
echo '<table align="center" border="0" bordercolor="#666666" cellpadding="5" cellspacing="3">
<tr class="tableTop">
<td>Account Code</td>
<td>Debit</td>
<td>Credit</td>';
// get total debit
foreach((array_filter($diff, "debit")) as $x){
$totalDebit += $x;
}
// get total credit
foreach((array_filter($diff, "credit")) as $y){
$totalCredit += $y;
}
do {
$diff = array($row_amounts['difference']);
echo '
</tr>
<tr class="tableButtom">
<td>'.$row_amounts['Account_Code'].'</td>
<td>';
foreach((array_filter($diff, "debit")) as $new){
echo abs($new).'.00';}
echo '</td>
<td>';
foreach((array_filter($diff, "credit")) as $new2){
echo $new2;
}
echo '</td>';
}while ($row_amounts = mysql_fetch_array($amounts));
echo '</tr>
<tr class="tableButtom">
<td>';
echo 'Total';
echo '</td>
<td>';
echo abs($totalDebit).'00';
echo '</td>
<td>';
echo $totalCredit.'00';
echo '</td>
</tr>
</table>';
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.