我在下面写的代码有问题。基本上,当购物车中有x个商品时,它会回显文本"You have x item(s) in the cart“。然而,当没有商品时,它应该回显“你购物车中没有任何商品”,而不是什么都不回显。我做错了什么?
<?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."; } 
    }
?>发布于 2013-04-13 06:17:03
这是因为代码没有出现在for each循环中。
<?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。
发布于 2013-04-13 06:18:05
删除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.";https://stackoverflow.com/questions/15981684
复制相似问题