首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果购物车项目=0,则显示文本

如果购物车项目=0,则显示文本
EN

Stack Overflow用户
提问于 2013-04-13 06:11:51
回答 2查看 92关注 0票数 0

我在下面写的代码有问题。基本上,当购物车中有x个商品时,它会回显文本"You have x item(s) in the cart“。然而,当没有商品时,它应该回显“你购物车中没有任何商品”,而不是什么都不回显。我做错了什么?

代码语言:javascript
运行
复制
<?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."; } 
    }
?>
EN

Stack Overflow用户

回答已采纳

发布于 2013-04-13 06:17:03

这是因为代码没有出现在for each循环中。

代码语言:javascript
运行
复制
<?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。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15981684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档