无法在PHP中重新计算列的总和 [英] Unable to recalculate the total sum of a column on filtering in PHP

查看:91
本文介绍了无法在PHP中重新计算列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在为一些代码而苦苦挣扎.我正在尝试获取表中特定列的总数,下面是我的代码.

I am really struggling with some code. I am trying to get the total of a specific column in a table, my code is below.

它的工作方式是为我提供列的总数-但是当我过滤表格时,总数保持不变,并且在过滤时不会改变.

It works in terms of providing me the total of the column - but when I filter the table, the total amount remains the same and doesn't change when filtered.

例如,当我加载页面时- transaction_amount 列的总和为99-但是当我对其进行过滤以在过滤器中搜索其他帐户时,它仍然会抛出99作为和.这可能真的很简单,但是我已经苦苦挣扎了一段时间.希望您能提供帮助吗?

For example, when I load the page - the sum of the transaction_amount column amounts to 99 - but when I filter this to search for a different account in the filter, it still throws 99 as the sum. This is probably really simple, but I have been struggling for some time now. Hoping you can help?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $('table thead th').each(function(i) {
            calculateColumn(i);
        });
    });

    function calculateColumn(index) {
        var total = 0;
        $('table tr').each(function() {
            var value = parseInt($('td', this).eq(index).text());
            if (!isNaN(value)) {
                total += value;
            }
        });
        $('table tfoot td').eq(index).text('Total: ' + total);
    }
</script>
</head>
<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>  
  <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
 </head>
 <body>
  <div class="container box">
   <h3 align="center">How to Get SUM with Datatable Server-side-processing in PHP</h3>
   <br />

 <table id="example1" class="table table-bordered table-striped">
     <thead>
         <tr>
            <th> Date</th>
            <th>Account</th>
            <th> Transaction Name</th>
            <th> Type</th>
            <th>Method</th>
            <th>Amount</th>
            <th> More</th>
           </tr>
    </thead>
    <?php
    if ($result->num_rows > 0) 
    {        
      while($row = $result->fetch_assoc()) {
        ?>
            <tr class="table table-bordered table-striped" id="row-<?php echo $row["id"]; ?>"> 
                <td style="width: 11%" class="table table-bordered table-striped"><?php echo $row["submitted_date"]; ?></td>
                <td style="width: 11%"class="table table-bordered table-striped" > <?php echo $row["posting_account_number"]; ?></td>
                <td style="width: 45%"class="table table-bordered table-striped"><?php echo $row["transaction_name"]; ?></td>
                <td style="width: 22%"class="table table-bordered table-striped"><?php echo $row["method"]; ?></td>
                <td width="11%"class="table table-bordered table-striped"><?php echo $row["type"]; ?></td>
                <td class="table table-bordered table-striped"><?php echo $row["transaction_amount"]; ?></td>
                <td class="table table-bordered table-striped" align = "" colspan="2">
                  <a <a href="" type="link" class="" data-toggle="modal" data-target="#modal-default<?php echo $row["id"]; ?>"> Details</a> </a>

        <tfoot>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>Total:</td>
                <td></td>
            </tr>
        </tfoot>

推荐答案

您的代码中的HTML代码中有很多错误……我想知道它是如何运行的. </head>被写入两次. <a>添加在另一个<a>中. <tr>未关闭.在最后.其他的,您还没有结束循环.也许这就是为什么您本来会遇到这些问题的原因.休息,我们需要检查过滤器代码以详细说明.

There are a lot of errors in HTML code in your code ... I wonder how it's running. </head> is written twice. <a> is added inside another <a>. <tr> is not closed. at the end. And other you have not closed the loop. Maybe that's why you could have been facing these issues. Rest, We need to check the filter code to tell in detail.

我已经在下面纠正了您的问题.

I have rectified your issues below.

  <script>
    $(document).ready(function() {
        $('table thead th').each(function(i) {
            calculateColumn(i);
        });
    });

    function calculateColumn(index) {
        var total = 0;
        $('table tr').each(function() {
            var value = parseInt($('td', this).eq(index).text());
            if (!isNaN(value)) {
                total += value;
            }
        });
        $('table tfoot td').eq(index).text('Total: ' + total);
    }
</script>

<table id="example1" class="table table-bordered table-striped">
    <thead>
       <tr>
          <th>Date</th>
          <th>Account</th>
          <th>Transaction Name</th>
          <th>Type</th>
          <th>Method</th>
          <th>Amount</th>
          <th>More</th>
       </tr>
    </thead>
    <tbody>
        <?php
           if ($result->num_rows > 0) 
           {        
               while($row = $result->fetch_assoc()) 
               {
                  ?>
                   <tr class="table table-bordered table-striped" id="row-<?php echo $row["id"]; ?>">
                       <td style="width: 11%" class="table table-bordered table-striped"><?php echo $row["submitted_date"]; ?></td>
                       <td style="width: 11%"class="table table-bordered table-striped" > <?php echo $row["posting_account_number"]; ?></td>
                       <td style="width: 45%"class="table table-bordered table-striped"><?php echo $row["transaction_name"]; ?></td>
                       <td style="width: 22%"class="table table-bordered table-striped"><?php echo $row["method"]; ?></td>
                       <td width="11%"class="table table-bordered table-striped"><?php echo $row["type"]; ?></td>
                       <td class="table table-bordered table-striped"><?php echo $row["transaction_amount"]; ?></td>
                       <!-- action -->
                       <td class="table table-bordered table-striped" align = "" colspan="2">
                          <a href="" type="link" class="" data-toggle="modal" data-target="#modal-default<?php echo $row["id"]; ?>"> Details</a>
                       </td>
                  </tr>
                  <?php
               }
        ?>
    </tbody>
    <tfoot>
       <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td>Total:</td>
          <td></td>
       </tr>
    </tfoot>
</table>

根据您的要求更新答案:

Updated Answer as per your requirement:

给一个类一个数量,然后在js函数中,仅添加该类的数据.

<script>
    $(document).ready(function() {
        $('table thead th').each(function(i) {
            calculateColumn('.cal_amt');
        });
    });

    function calculateColumn(index) {
        var total = 0;
        $(index).each(function() {
            var value = parseInt($(this).text());
            if (!isNaN(value)) {
                total += value;
                console.log(total);
            }
        });
        $('.show_amt').text('Total: ' + total);
    }
</script>

<table id="example1" class="table table-bordered table-striped">
    <thead>
       <tr>
          <th>Date</th>
          <th>Account</th>
          <th>Transaction Name</th>
          <th>Type</th>
          <th>Method</th>
          <th>Amount</th>
          <th>More</th>
       </tr>
    </thead>
    <tbody>
        <?php
           if ($result->num_rows > 0) 
           {        
               while($row = $result->fetch_assoc()) 
               {
                  ?>
                   <tr class="table table-bordered table-striped" id="row-<?php echo $row["id"]; ?>">
                       <td style="width: 11%" class="table table-bordered table-striped"><?php echo $row["submitted_date"]; ?></td>
                       <td style="width: 11%"class="table table-bordered table-striped" > <?php echo $row["posting_account_number"]; ?></td>
                       <td style="width: 45%"class="table table-bordered table-striped"><?php echo $row["transaction_name"]; ?></td>
                       <td style="width: 22%"class="table table-bordered table-striped"><?php echo $row["method"]; ?></td>
                       <td width="11%"class="table table-bordered table-striped cal_amt"><?php echo $row["type"]; ?></td>
                       <td class="table table-bordered table-striped"><?php echo $row["transaction_amount"]; ?></td>
                       <!-- action -->
                       <td class="table table-bordered table-striped" align = "" colspan="2">
                          <a href="" type="link" class="" data-toggle="modal" data-target="#modal-default<?php echo $row["id"]; ?>"> Details</a>
                       </td>
                  </tr>
                  <?php
               }
        ?>
    </tbody>
    <tfoot>
       <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td class="show_amt">Total:</td>
          <td></td>
       </tr>
    </tfoot>
</table>

这篇关于无法在PHP中重新计算列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆