如果购物车表为空,则隐藏购物车表


Hiding shopping cart table if empty

我正在开发一个包含订单页面和购物车的网站,我一直在尝试弄清楚如果购物车中没有商品,如何隐藏购物车的表格。

到目前为止,我已经尝试过这样做,但我所做的只是隐藏表格,无论之前是否已将商品添加到购物车中。

以下是订单页面中的相关代码:

<h2> Just a bite </h2>
 <div id="table1">
     <table border="0" cellspacing="0" cellpadding="2" width="500">
         <tr>
           <td colspan="7" class="pageName">Please select from our starters below</td>
           <br>
         </tr>
         <tr>
           <td width="22%" height="110"><img src="shrimp.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
           <td>&nbsp;</td>
           <td width="22%" height="110"><img src="potatoskins.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
           <td>&nbsp;</td>
           <td width="22%" height="110"><img src="salad.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
           <td>&nbsp;</td>
      <      td width="22%" height="110"><img src="bread.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
        </tr>
         <tr>
           <td class="detailText" valign="top" nowrap="nowrap"> Sticky BBQ Prawns<br><br>
           £5.00 <br><br> <a href="order.php?add=Sticky BBQ Prawns&price=5.00&qty=1"><button>Add to Cart</button></a><br><br><img src="1chilli.png" width="20" height="20"></td>
           <td>&nbsp;</td>
            <td class="detailText" valign="top" nowrap="nowrap"> Potato Skins<br><br>
             £4.00 <br><br> <a href="order.php?add=Potato Skins&price=4.00&qty=1"><button>Add to Cart</button></a></td>
          <td>&nbsp;</td>
            <td class="detailText" valign="top" nowrap="nowrap"> Caesar Salad<br><br>
            £3.00 <br><br> <a href="order.php?add=Caesar Salad&price=3.00&qty=1"><button>Add to Cart</button></a><br><br><img src="vege.png" width="20" height="20"></td>
          <td>&nbsp;</td>
            <td class="detailText" valign="top" nowrap="nowrap"> Fresh Bread Selection<br><br>
            £1.00 <br><br> <a href="order.php?add=Fresh Bread Selection&price=1.00&qty=1"><button>Add to Cart</button></a><br><br><img src="vege.png" width="20" height="20"></td>
        </tr>
         <tr>
             <td colspan="7">&nbsp;</td>
         </tr>
         </table>
 </div>

以下是购物车页面中的相关代码:

<?php
session_start();
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }

if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
    $ITEM = array(
        'name' => $_GET['add'], 
        'price' => $_GET['price'], 
        'qty' => $_GET['qty']       
        );
    $_SESSION['SHOPPING_CART'][] =  $ITEM;
    header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['remove'])){
    unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
    header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['empty'])){
    session_destroy();
    header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_POST['update'])) {
    foreach ($_POST['items_qty'] as $itemID => $qty) {
        if ($qty == 0) {
            unset($_SESSION['SHOPPING_CART'][$itemID]);
        }
        else if($qty >= 1) {
            $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
        }
    }
    header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
...
    <h1> Order Cart </h1>
<br><br>
<?php $totalPrice = 0;
if ($totalPrice > 0) { ?>
<div id="shoppingCartDisplay">
<form action="" method="post" name="shoppingcart">
    <table width="680" border="2">
      <tr>
        <th scope="col">Remove Item/s</th>
        <th scope="col">Item Name</th>
        <th scope="col">Item Price</th>
        <th scope="col">Qty</th>
        <th scope="col">Cost</th>
      </tr>
    <?php } ?>
    <?php 
    foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
    ?>
    <tr id="item<?php echo $itemNumber; ?>">    
        <td><a href="?remove=<?php echo $itemNumber; ?>"><img src="x.png"></a></td>
        <td><?php echo $item['name']; ?></td>
        <td>£<?php echo $item['price']; ?></td>
        <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="3" maxlength="3" /></td>
        <td>£<?php echo $item['qty'] * $item['price']; ?></td>
        <?php $totalPrice += (($item['qty']>=0?$item['qty']:0) * $item['price']); ?>
    </tr>
    <?php
    }
    ?>
</table>

如果有人对我如何获得这项工作有任何建议,请告诉我:)

那么你需要的是检查购物猫是否不为空显示你的代码。

<?php
if(!empty($_SESSION['SHOPPING_CART'])){ ?>
 <!-- display table of product or whatever -->
<?php } else { ?>
<!-- Tell user to add product in cart first  -->
<?php } ?>

希望会有所帮助