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条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划