数组列的总和

Anyone here can help me solve this? I just want to get the total or sum of certain column, please refer to the image below. Been trying to sort this for 2days but cant get no luck, Hope anyone can help me with this. I appreciate it so much, and thanks in advance.

Here is a sample image http://www.freeimagehosting.net/pig81

<?php $sql = "SELECT name, doc_date, descs, debit, credit FROM statement WHERE  
member_id = $member_id ORDER BY doc_date";

$query = mysql_query($sql);
$combinedResults = array();

while($result = mysql_fetch_array($query)) {
$combinedResults[$result[`name`]][] = array(`name` => $result[`name`], `doc_date` =>   
$result[`doc_date`], `descs` => $result[`descs`],`debit` => $result[`debit`], `credit` 
=> $result[`credit`]);}

foreach(array_keys($combinedResults) as $groupKey) { ?>
<table>
  <tr><?php  foreach($combinedResults[$groupKey] as $item) {?>
    <td>Date</td>
    <td>Description</td>
    <td>Debit</td>
    <td>Credit</td>
    <td>Balance</td>
  </tr>
<tr>
<td colspan="2"><?php  echo $groupKey; ?></td>
<td width="105">&nbsp;</td>
<td width="105">&nbsp;</td>
<td width="105">&nbsp;</td>
</tr>
<tr><?php  foreach($combinedResults[$groupKey] as $item) {?>
<td><?php echo $item[`doc_date`];?></td>
<td><?php echo $item[`descs`];?></td>
<td><?php echo $item[`debit`];?></td>
<td><?php echo $item[`credit`]; ?></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>sum of debit goes here</td>
</tr>
<?php }} ?>
</table>

I've refactored the code based on what I see in it, and added a balance calculator, but I haven't actually tested it.

<?php

$sql = "SELECT name, doc_date, descs, debit, credit
        FROM statement
        WHERE  member_id = $member_id
        ORDER BY doc_date";

$query = mysql_query($sql);
$combinedResults = array();

// Slurp SQL results into array
while ($result = mysql_fetch_array($query)) {
  $combinedResults[$result['name']][] = array(
    'name' => $result['name'],
    'doc_date' => $result['doc_date'],
    'descs' => $result['descs'],'debit' => $result['debit'],
    'credit' => $result['credit']
  );
}

// Define a format for all table lines (add CSS as required)
$fmt = "<tr>
  <td>%s</td>
  <td>%s</td>
  <td>%s</td>
  <td>%s</td>
  <td>%s</td>
</tr>";

print "<style type='text/css'>TD{width:105px;}</style>
";

print "<table>
";

// Walk through array...
foreach ($combinedResults[$groupKey] as $item) {
  // Start a section...
  printf($fmt, "Date", "Description", "Debit", "Credit", "Balance");
  printf($fmt, $groupKey, "", "", "", "");
  $balance = 0; // Initialize the balance for this section...
  foreach ($combinedResults[$groupKey] as $item) {
    printf($fmt, $item['doc_date'], $item['descs'], $item['debit'], $item['credit'], "");
    $balance += $item['debit'];
  }
  printf($fmt, "", "", "", "", $balance); // Print the balance.
}

print "</table>
";

I'm interested to know if it works. :)

Note that I didn't make allowances for your "colspan"; I suspect that you should settle on your logic before you try to build it into an actual layout.

You can change your SQL Statement with something like

SELECT name, doc_date, descs, debit, credit, SUM(debit) AS sum FROM statement WHERE member_id = $member_id ORDER BY doc_date

You then print it with

<?php echo $item['sum']; ?>

You might also want to have a look either at PDO and prepared statements that replace mysql_ functions.