I finished getting the shopping cart organize. Every time i refresh the page (under the shopping_cart.php) an addition quantity is added by increment of 1 (changing the final price as well). How do I stop this from happening?
shopping_cart.php
<?php
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>
functions.inc.php
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>Shopping Cart <a href="shopping_cart.php">'.count($items).' item'.$s.' </a></p>';
}
}
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="shopping_cart.php?action=update" method="post" id="cart">';
$output[] = '<table border="0" width="100%" cellspacing="0" cellpadding="0">';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM Products WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td width="217" align="center">
<a class="right_20" href="product_info.php?products_id='.$id.'">
<img src="images/product_5.jpg" alt="Consectetur adipiscing" title="Consectetur adipiscing"
width="211" height="211" /></a></td>';
$total += $price * $qty;
$output[] = '<td width="414" valign="top"><a href="product_info.php?products_id='.$id.'">
<strong>Consectetur adipiscing</strong></a>
<span class="markProductOutOfStock">***</span><br /><br />
<input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" />
<div><button type="submit">Update cart</button> or
<a href="shopping_cart.php?action=delete&id='.$id.'" class="r">Remove</a></div></td>';
$output[] = '<td width="207" align="right" valign="top"> <strong>$'.($price * $qty).'</strong></td>';
$output[] = '</tr>';
}
$output[] = '<tr>';
$output[] = '<td colspan="2"></td>';
$output[] = '<td ><p align="right">Grand total: <strong>$'.$total.'</strong></p></td>';
$output[] = '</tr>';
$output[] = '<tr>';
$output[] = '<td colspan="3"><p class="stockWarning" align="center">Products marked with *** dont
exist in desired quantity in our stock.<br />You can buy them anyway and check the
quantity we have in stock for immediate deliver in the checkout process.</p></td>';
$output[] = '</tr>';
$output[] = '</table>';
$output[] = '</form>';
} else {
$output[] = '<p><a href="products_new.php">Continue Shopping</a></p>';
}
return join('',$output);
}
?>