如果空的话,隐藏购物车表

时间:2014-12-09 20:26:12

标签: php html-table hide

我正在一个包含订单页面和购物车的网站上工作,如果购物车中没有商品,我一直试图弄清楚如何隐藏购物车的桌子。

到目前为止,我已经尝试了这一点,但我的所有尝试都是隐藏表格,无论之前是否已将某个项目添加到购物车中。

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

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

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

1 个答案:

答案 0 :(得分:2)

然后您需要检查购物猫是否为空显示您的代码。

<?php
if(!empty($_SESSION['SHOPPING_CART'])){ ?>
 <!-- display table of product or whatever -->

<?php } else { ?>
<!-- Tell user to add product in cart first  -->
<?php } ?>

希望会有所帮助