我遇到了我在下面写的代码问题。基本上,当购物车中有x个商品时,它会回显“购物车中有x个商品”的文字。然而,当没有物品时,它应该回应“你没有购物车中的任何物品”,而是回应什么。我做错了什么?
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
foreach($array->products as $product){
$amount = $product->amount;
if ($amount != 0){ echo "You have $amount item(s) in the cart."; }
else { echo "You don't have any items in the cart."; }
}
?>
答案 0 :(得分:0)
因为代码不会出现在每个循环中。
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
if (count($array->products) > 0) {
foreach($array->products as $product){
$amount = $product->amount;
echo "You have $amount item(s) in the cart.";
/* Do other thinks here. */
}
} else {
echo "You don't have any items in the cart.";
}
?>
我不确定你为什么要使用循环btw。
答案 1 :(得分:0)
删除你的foreach并使用此
if($size=sizeof($array->products))
echo "You have ".$size." item(s) in the cart.";
else
echo "You don't have any items in the cart.";