doubairan4213 2013-04-12 22:11
浏览 11
已采纳

如果购物车商品= 0,则显示文字

i'm having issues with the code i wrote up below. basically, when there are x items in the cart, it echoes the text "You have x item(s) in the cart". However, when there are no items, it should echo "You don't have any items in the cart" but instead echoes nothing. What am i doing wrong?

<?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."; } 
    }
?>
  • 写回答

2条回答 默认 最新

  • dongyaxiao5884 2013-04-12 22:17
    关注

    Its because the code does not come in the for each loop.

    <?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."; 
        } 
    ?>
    

    I'm not sure why you wanne use a loop btw.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?