Oscar Oscar - 1 year ago 68
PHP Question

Hiding shopping cart table if empty

I am working on a website which contains an order page and shopping cart, I have been trying to work out how I might be able to hide the table for the shopping cart if there are no items in the cart.

So far I have attempted this but all my attempt does is hide the table regardless if an item has been previously added to the cart.

Here is the relevant code from the order page:

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


Here is the relevant code from the shopping cart page:

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


If anyone has any suggestions as to how I can get this work let me know :)

Answer Source

then what you need there is to check if shopping cat is not empty display your code.

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

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

Hope will help

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download