php - 尝试在购物车为空时显示消息
问题描述
我使用数组创建了一个购物车。我正在尝试输出“您的购物车是空的”。数组为空时的消息。当我尝试这个时,它似乎对我不起作用......
<?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; ?>
解决方案
使用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; ?>
推荐阅读
- google-cloud-platform - GCS:如何以存储对象管理员身份上传到存储桶
- javascript - javascript mapobject中的正则表达式
- python - 如何检查 python/tk 中条目小部件的内容是浮点数、字符串、布尔值还是整数?
- sql - SQL Server 邮件变量用法
- java - 模板方法和继承或组合
- php - 向上和向下舍入为 0.5,如果在点文档后为 0
- java - Fragment给了我白屏
- php - 在codeigniter中使用form_error函数后,html消失
- reactjs - 反应路由更改后如何重新安装组件
- ruby-on-rails - 如何使用 rubyzip gem 压缩列表临时文件