douyi1966
2012-04-25 17:17
浏览 30
已采纳

数组列的总和

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>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanji9378 2012-04-25 19:06
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drutcs7210 2012-04-25 17:26

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题