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

数组列的总和

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效