dselp3944 2019-03-22 06:26
浏览 52

使用PHP - 检查购物车是否为空,如果是,则显示消息

I'm new to php so I've struggled through each step of this assignment. I've hit a wall with this final step. I've built a simple shopping cart using only PHP (per the instructions). The index.php page contains all the items as well as a checkout button that redirects to the checkout.php page. If the user clicks this "checkout" button before items have been added to their cart, I need to display "Your cart is empty" message on the checkout page. I'm assuming I need to do the check on the checkout.php file. I've attempted it more than a dozen times without luck. Help is appreciated.

I'm including my attempted code on the checkout.php file

<?php

require_once('session_util.php'); 

if(isset($_SESSION[SHOPPING_CART]) && empty($_SESSION[SHOPPING_CART])) {
    echo 'Your shopping cart is empty.';
}
?>

<!doctype html>
<html lang="en">
<head>
    <title>Form 1</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<main>
    <h2>Checkout</h2>
    <table id="cart">
        <?php
        // This is where session starts
        $itemArray = $_SESSION[SHOPPING_CART]->getItemArray();
        foreach ($itemArray as $item) {
            $name = $item->getName();
            $price = number_format($item->getPrice(), 2);

            echo "<tr>
                    <th>$name</th>
                    <th>$$price</th>
                  </tr>";
        }

        // you will get these from the shopping cart, not a specifc item
        $subTotal = number_format($_SESSION[SHOPPING_CART]->subTotal(), 2);
        $tax = number_format($_SESSION[SHOPPING_CART]->addTax(), 2);
        $total = number_format($_SESSION[SHOPPING_CART]->getTotal(), 2);

        // Display calculated subtotal, tax, and total.
        ?>
        <?php
        echo "
            <table id='total'>
            <tr><td>SubTotal &nbsp $$subTotal</td></tr>
            <tr><td>Tax &nbsp &nbsp &nbsp &nbsp &nbsp $$tax</td></tr>
            <tr><td>Total &nbsp &nbsp &nbsp &nbsp $$total</tr></td>
            </table>
            ";
        ?>
    </table>
    <form id="keep_shopping" method="post" action="index.php">
        <input id="submit" type="submit" class="keep_shopping" name="keep_shopping" value="Keep Shopping">
    </form>
    <form id="empty_cart" method="post" action="clear_cart.php">
        <input id="submit" type="submit" class="empty_cart" name="empty_cart" value="Empty Cart">
    </form>
</main>
</body>
</html>

Edits and Infromation as asked in comment:
enter code heresession_start() is located in a separate file - session_util.php - and this file is required at the top of all other assignment files.

Also, when I do a var_dump on $_SESSION[SHOPPING_CART]; I get the following:

object(ShoppingCart)#11 (1) { ["itemArray":"ShoppingCart":private]=> array(0) { } }

  • 写回答

1条回答 默认 最新

  • drpqxogph15436713 2019-03-22 06:41
    关注

    First of all there must be

    session_start() on the php pages where you are accessing the session variables.

    Then there is a syntax error, instead of

    $_SESSION[SHOPPING_CART] it should be $_SESSION["SHOPPING_CART"] everywhere throughout the code.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大