Jump to content

Whats wrong here


menios

Recommended Posts

This is the Code for my cart.All works fine but one details spoils everything and i can't figure the mistake.When a new item is added to the cart there are 2 buttons next to it add and remove.All fine here but when i try to add a second item in the cart the add and remove buttons appear but my add button actually works as a remove.

Any suggestions on what i m doing wrong?

This is my functions

[hr]<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>Your Cart is Empty</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>Your Cart contains <a href="cart.php">'.count($items).' book'.$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="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table  border="0">';
	$output[] = '<tr>';
	$output[] = '<th bgcolor=#62aac4><strong>Book</strong></th>';
	$output[] = '<th bgcolor=#62aac4>Price</th>';
	$output[] = '<th bgcolor=#62aac4>Quantity</th>';
	$output[] = '<th bgcolor=#62aac4 colspan=3> </th>';

	$output[] = '</tr>';
	foreach ($contents as $pd_id=>$qty) {
		$sql = 'SELECT * FROM tbl_product WHERE pd_id = '.$pd_id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
					$output[] = '<td align=center><strong>'.$pd_name.'</strong></td>';
		$output[] = '<td align=center>£'.$pd_price.'</td>';
		$output[] = '<td align=right ><input type="text" name="qty'.$pd_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td align=right><i>£'.($pd_price * $qty).'</i></td>';
		$total += $pd_price * $qty;
		$output[] = '<td align=center><button type="submit">Add</button></td>';
		$output[] = '</form>';
		$output[] = '<form action="cart.php?action=delete&pd_id='.$pd_id.'" method="post" id="cart">';
		$output[] = '<td align=center><button type="submit" class="r">Remove</button></td>';
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p><pre><strong><i>                             Total: <big></i>£'.$total.'</strong></big></pre></p>';

	$output[] = '</form>';
	$output[] = '<pre>                                <a href="testform/forma.php" target="_top">Checkout</a></pre>';
} else {
	$output[] = '<p>Your Cart is empty.</p>';
}
return join('',$output);
}
?>

And this is the cart

[hr]<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/cartcon.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['pd_id'];
	} else {
		$cart = $_GET['pd_id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['pd_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')) {
			$pd_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 .= ','.$pd_id;
				} else {
					$newcart = $pd_id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/styles.css">
</head>

<body>

<div id="contents">
<h1>Your Shopping Cart</h1>
<?php
echo writeShoppingCart();
echo showCart();
?>
<p><a href="iframe.html" target="display">Continue Shopping</a></p>

</div>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.