计算列php的总和

时间:2012-09-05 12:37:11

标签: php mysql sum

我有以下代码:

<td class="dataTableContent">
<?php
$cuttinglist_weight_query = tep_db_query("select op.orders_id, p.products_id, SUM(p.products_weight) as total_products_weight from " . TABLE_ORDERS_PRODUCTS . " op  left join " . TABLE_PRODUCTS . " p on (op.products_id = p.products_id) where orders_id = '" . (int)$cuttinglist['orders_id'] . "'");
$cuttinglist_weight = tep_db_fetch_array($cuttinglist_weight_query);

echo $cuttinglist_weight['total_products_weight'];

?>

</td>

这将打印订单总重量列:

enter image description here

在下面标题为“准备总重量”的框中,我需要显示该列的总数,是否有人有任何想法?

1 个答案:

答案 0 :(得分:2)

<td class="dataTableContent">

    <?php
    $cuttinglist_weight_query = tep_db_query("select op.orders_id, p.products_id, SUM(p.products_weight) as total_products_weight from " . TABLE_ORDERS_PRODUCTS . " op  left join " . TABLE_PRODUCTS . " p on (op.products_id = p.products_id) where orders_id = '" . (int)$cuttinglist['orders_id'] . "'");
    $cuttinglist_weight = tep_db_fetch_array($cuttinglist_weight_query);

    echo $cuttinglist_weight['total_products_weight'];

    if(!$totalSum){ $totalSum = 0; }
    $totalSum += $cuttinglist_weight['total_products_weight']
    ?>

</td>

然后在你的单元格的最后回复它:

<table>
    <tr>
        <td>Total Weight To Prepare</td>
    </tr>
    <tr>
        <td><?php echo $totalSum; ?></td>
    </tr>
</table>