Reputation: 541
I am currently working on a website for a takeaway restaurant, I have a menu page where the customer adds their chosen food to their cart, when they click "Add to cart", they are sent to the cart page which displays the items they have added.
Currently, the name of item, price and quantity is displayed, however I wish to add a total cost sum at the bottom of their cart.
Here is the relevant code present on my menu 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> </td>
<td width="22%" height="110"><img src="potatoskins.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
<td> </td>
<td width="22%" height="110"><img src="salad.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
<td> </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> </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> </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> </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"> </td>
</tr>
</table>
</div>
Here is the relevant code on my cart page, including the area I wish for the total price to be displayed:
<?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']);
}
?>
...
<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
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>
</tr>
<?php
}
?>
</table>
Total price: <?php echo $itemNumber['price'] + $item['price']; ?>
<?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
<p>
<label>
<input type="submit" name="update" id="update" value="Update Cart" />
</label>
</p>
</form>
As you can see I have attempted at trying to get this work but I really am not sure :( If anyone has any suggestions I would appreciate the help! Thank you :)
Upvotes: 1
Views: 2733
Reputation: 81
$itemNumber and $item are only accessible inside of the foreach.
try
<?php $totalPrice = 0; ?>
<?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>
Total price: <?php echo $totalPrice; ?>
And I totaly Agree with Jason. You should remove the price form the get parameters. They are easily being used to order for free. Also check, if the quantiy is not negative.
Upvotes: 2