首页 > 解决方案 > 尝试在购物车为空时显示消息

问题描述

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

<?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; ?>

标签: phpsqlarraysif-statement

解决方案


使用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; ?>

推荐阅读