以下代码显示订购产品的个别重量:
$weightsql = 'select op.products_name, op.products_quantity , p.products_weight from ' . TABLE_ORDERS_PRODUCTS . ' op left join ' . TABLE_PRODUCTS . ' p on op.products_id = p.products_id where op.products_id = '.$pro['products_id'];
$weightq = tep_db_query( $weightsql );
while ($weight = tep_db_fetch_array( $weightq )){
if($category_parent_id != 0)$list_items[] = $weight['products_weight'].' ';
}
这显示了订购的每种产品的重要性。我坚持的不是显示单独的重量,而是需要显示总重量。例如,如果产品订购了三次,重量为7kg,那么目前的代码显示:
Product 7.00 7.00 7.00
如何让它显示21公斤的总重量?
答案 0 :(得分:3)
array_sum() examples
<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>
The above example will output:
sum(a) = 20
sum(b) = 6.9
答案 1 :(得分:0)
在循环之前将计数器设置为0并在循环期间添加它,如下所示:
$total = 0;
$weightsql = 'select op.products_name, op.products_quantity , p.products_weight from ' . TABLE_ORDERS_PRODUCTS . ' op left join ' . TABLE_PRODUCTS . ' p on op.products_id = p.products_id where op.products_id = '.$pro['products_id'];
$weightq = tep_db_query( $weightsql );
while ($weight = tep_db_fetch_array( $weightq )){
if($category_parent_id != 0) {
$list_items[] = $weight['products_weight'].' ';
$total += $weight['products_weight'];
}
}
变量$ total应该返回21。
答案 2 :(得分:0)
如果您按照ID选择了产品,最简单的解决方案是将行数乘以权重。无需将其放入数组中。
答案 3 :(得分:0)
$weightsql = 'select op.products_name, sum(op.products_quantity * p.products_weight) as weightsum from ' . TABLE_ORDERS_PRODUCTS . ' op left join ' . TABLE_PRODUCTS . ' p on op.products_id = p.products_id where op.products_id = '.$pro['products_id'];
echo $weight['weightsum']
感谢您的帮助(!)