我正在使用下面的代码根据购物车中的商品数量显示一个数字。如果有1个项目,则生成数字1,显示2个项目和2个等等。
问题是它显示ID为[1]的产品数量。如何改变它以使其适用于所有产品ID?<?php $array = unserialize($_SESSION['__vm']['vmcart']);
$amount = $array->products[1]->amount;
if ($amount != 0){ echo $amount; } else { echo 0; } ?>
[1]是产品ID。如何更改它以接受所有产品ID?
答案 0 :(得分:1)
您想要遍历所有产品吗?有点像...
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
foreach($array->products as $product){
$amount = $product->amount;
if ($amount != 0){ echo $amount; } else { echo 0; }
}
?>
添加所有产品......
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
$total = 0;
foreach($array->products as $product){
$total += $product->amount;
}
echo "Total Products: " . $total;
?>