【问题标题】:Trying to display a message when the shopping cart is empty尝试在购物车为空时显示消息
【发布时间】:2018-05-03 06:51:51
【问题描述】:

我使用数组创建了一个购物车。我正在尝试输出“您的购物车是空的”。数组为空时的消息。当我尝试这个时,它似乎对我不起作用......

<?php 
            if (isset($_SESSION['shopping_cart'])):
            if (count($_SESSION['shopping_cart']) < 1):
         ?>
            <a>Your cart is empty.</a>

         <?php endif; endif; ?>

<?php 
            if (isset($_SESSION['shopping_cart'])):
            if (count($_SESSION['shopping_cart']) > 0):
         ?>
            <a href="checkout.php" class="check-button">Checkout</a>

         <?php endif; endif; ?>

【问题讨论】:

  • 请回显$_SESSION['shopping_cart']并在此处显示输出

标签: php sql arrays if-statement


【解决方案1】:

使用empty() 而不是isset

<?php if (empty($_SESSION['shopping_cart'])): ?>

<a>Your cart is empty.</a>

<?php endif; ?>

<?php if ( ! empty($_SESSION['shopping_cart'])):?>

<a href="checkout.php" class="check-button">Checkout</a>

<?php endif; ?>

【讨论】:

  • 我刚试过,当数组为空时,我仍然没有得到任何文本输出。
  • 尝试var_dump($_SESSION['shopping_cart']) 并在此处发布结果。如果没有结果,请在初始化 $_SESSION['shopping_cart'] 的位置添加代码
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2019-09-01
  • 1970-01-01
相关资源
最近更新 更多