dongmoxin7111 2018-08-14 15:17
浏览 22
已采纳

如何使用php动态回显日期范围搜索结果中列数据的总和?

I have a search feature that displays records from a database based on date range search. What I want to do is echo the total sum of the "refund_amount" column that is displayed after the date range search. I know how to get the total sum from all the records from the database. I need to echo only the sum of the "refund_amount" column that is being displayed after the date range search. Any help is greatly appreciated.

Here is my code:

<?php
  include('includes/connect.php');
  if (isset($_GET["status"])) { $status  = $_GET["status"]; } else { $status=0; };
  if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=0; };
  if (isset($_GET["d2"])) { $d2  = $_GET["d2"]; } else { $d2=0; };
  $result = $connect->prepare("SELECT * FROM rma_list WHERE refund_date BETWEEN :a AND :b");
  $result->bindParam(':a', $d1);
  $result->bindParam(':b', $d2);
  $result->execute();
  for($i=0; $row = $result->fetch(); $i++){
  ?>

  <tr class="record">
  <td><?php echo $row['rma_id']; ?></td>
  <td><?php echo $row['order_date']; ?></td>
  <td><?php echo $row['rma_request_date']; ?></td>
  <td><?php echo $row['rma_number']; ?></td>
  <td><?php echo $row['order_number']; ?></td>
  <td><?php echo $row['so_number']; ?></td>
  <td><?php echo $row['vendor_name']; ?></td>
  <td><?php echo $row['vendor_ra']; ?></td>
  <td><?php echo $row['customer_name']; ?></td>
  <td><?php echo $row['credit_memo_number']; ?></td>
  <td><?php echo $row['refund_amount']; ?></td>
  <td><?php echo $row['refund_date']; ?></td>
  <td><?php echo $row['status']; ?></td>
  </tr>



  <?php
  }
  ?>
  • 写回答

1条回答 默认 最新

  • dpcyx08288 2018-08-14 15:31
    关注

    To do this in PHP code

    <?php
    include('includes/connect.php');
    if (isset($_GET["status"])) { $status  = $_GET["status"]; } else { $status=0; };
    if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=0; };
    if (isset($_GET["d2"])) { $d2  = $_GET["d2"]; } else { $d2=0; };
    $result = $connect->prepare("SELECT * FROM rma_list WHERE refund_date BETWEEN :a AND :b");
    $result->bindParam(':a', $d1);
    $result->bindParam(':b', $d2);
    $result->execute();
    
    // initialise a variable for the counter
    $tot = 0;
    
    while ($row = $result->fetch(PDO::FETCH_ASSOC)){
    ?>
          <tr class="record">
              <td><?php echo $row['rma_id']; ?></td>
              <td><?php echo $row['order_date']; ?></td>
              <td><?php echo $row['rma_request_date']; ?></td>
              <td><?php echo $row['rma_number']; ?></td>
              <td><?php echo $row['order_number']; ?></td>
              <td><?php echo $row['so_number']; ?></td>
              <td><?php echo $row['vendor_name']; ?></td>
              <td><?php echo $row['vendor_ra']; ?></td>
              <td><?php echo $row['customer_name']; ?></td>
              <td><?php echo $row['credit_memo_number']; ?></td>
              <td><?php echo $row['refund_amount']; ?></td>
              <td><?php echo $row['refund_date']; ?></td>
              <td><?php echo $row['status']; ?></td>
          </tr>
    <?php
    
        // accumulate the amount in the new var
        $tot += $row['refund_amount'];
    }
    ?>
        <tr class="totals">
            <td colspan="10"> TOTAL Refund</td>
            <td><?php echo $tot; ?></td>
            <td colspan="2">&nbsp;</td>
        </tr>
    

    The table layout may need a little work, but I hope you get the idea from this code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在hololens1上运行unity项目只有空窗口
  • ¥25 TABLEAU PREP无法打开
  • ¥15 百度帐号问题/centos
  • ¥15 关于#c语言#的问题:求完整代码条件好说
  • ¥100 HALCON DELPHI
  • ¥15 (需要远程,AI不回)VB6二进制文件转换成功,但是C#转换总是失败
  • ¥15 关于#matlab#的问题:有没有什么其他办法能够保证不退出进程(相关搜索:matlab调用)
  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码