每页中一列的总和

时间:2016-08-29 16:16:43

标签: javascript php yii javascript-events javascript-objects

here is the table 我想通过使用数据库的列名来打印每页的Amount列的总和,该列是表中的“amount_no”和“Amount”。它的意思是命名为,在前端“Amount”和后端“amount_no”。现在我想使用其中一个名称来打印列的总和而不是我在编码中使用的列号(4).....请帮助我作为我在php中的初学者。预先感谢 。!!

     <script type="text/javascript" class="init">


$(document).ready(function() {
$('#example').DataTable( {
    "footerCallback": function ( row, data, start, end, display ) {
        var api = this.api(), data;

        // Remove the formatting to get integer data for summation
        var intVal = function ( i ) {
            return typeof i === 'string' ?
                i.replace(/[\$,]/g, '')*1 :
                typeof i === 'number' ?
                    i : 0;
        };

        // Total over all pages
        total = api
            .column( 4 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Total over this page
        pageTotal = api
            .column( 4, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Update footer
        $( api.column( 4 ).footer() ).html(
            'Rs '+ pageTotal +' ( Rs '+ total +' total)'
        );
    }
  } );
} );


 </script>

php:

 <tfoot>
                    <tr>
                        <th colspan="5" style="text-align:right">Total:</th>
                        <th></th>
                    </tr>
                </tfoot>
 <tbody style="font-size:12px;">
<?php   
 while ($row = mysql_fetch_array($find))
 {
$id = $row['id'];
    $receipt_no = $row['receipt_no'];
$name = $row['name'];
$on_account = $row['on_account'];
    $deceased_name = $row['deceased_name'];
    $subDate = $row['subDate'];
    $others = $row['others'];
$amount_no = $row['amount_no'];
    $amount_word = $row['amount_word'];
$currency = $row['currency'];
$payment = $row['payment'];
$cheque_draft_no = $row['cheque_draft_no'];
$total_amount += $amount_no;


echo '<tr>';
    foreach($_POST['field'] as &$field_value) {
        switch ($field_value) {
            case '1':
                echo '<td><input name="selector[]" class="checkbox1"          id="edit-delete" type="checkbox" value="' . $id . '"></td>';
                break;
            case '2':
                echo '<td>' . $receipt_no . '</td>';
                break;
            case '3':
                echo '<td>' . $name . '</td>';
                break;
            case '4':
               echo '<td>' . $on_account. '</td>';
                break;
            case '5':
                echo '<td>' . $deceased_name. '</td>';
                break;
            case '6':
                echo '<td>' . $subDate. '</td>';
                break;
            case '7':
                echo '<td>' . $others. '</td>';
                break;
            case '8':

                echo '<td>' . $amount_no. '</td>';
                break;
            case '9':
                echo '<td>' . $amount_word. '</td>';
                break;
            case '10':
                echo '<td>' . $currency. '</td>';
                break;
            case '11':
                echo '<td>' . $payment. '</td>';
                break;
            case '12':
                echo '<td>' . $cheque_draft_no. '</td>';
                break;


        }
    }


?>
<?php echo '</tr>';

}

&GT;

1 个答案:

答案 0 :(得分:0)

实际上你有点难以理解你的意思,也许试着以不同的方式解释不同的分离代码等。

但据我所知,你有一个数据库,并且想要汇总列中的所有数据,为什么不在SQL中这样做呢?

SELECT SUM(column_name) FROM table_name;

http://www.w3schools.com/sql/sql_func_sum.asp