Jump to content

Shopping Cart


son
 Share

Recommended Posts

I am working on a simple shopping cart page with the following code:

if (isset($_REQUEST['do']) && ($_REQUEST['do'] == 'add')){	if (isset($_GET['id']))	{	$id = (int) $_GET['id'];		if ($id > 0)		{		$id_query = "SELECT product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{			list ($product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$id]))				{				$_SESSION['cart'][$id]++;				echo "<p>Another copy of $product with a price of $price has been added to your cart.</p>\n";				}				else				{				$_SESSION['cart'][$id] = 1;				echo "<p>$product with a price of $price has been added to your cart.</p>\n";				}			}		}	}}elseif (isset($_REQUEST['do']) && ($_REQUEST['do'] == 'update')){	foreach ($_POST['qty'] as $k => $v)	{	$pid = (int) $k;	$qty = (int) $v;		if ($qty == 0)		{		unset ($_SESSION['cart'][$pid]);		}		elseif ($qty > 0)		{		$_SESSION['cart'][$pid] = $qty;		}	}	echo "<p>Thecart page has been updated.</p>";}if ($_SESSION['cart'] && !empty($_SESSION['cart'])){$cart_query = "SELECT product, price FROM products WHERE product_id IN (";	foreach($_SESSION['cart'] as $id => $v)	{	$cart_query .= (int) $id . ',';	}$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY product";$cart_result = mysqli_query ($dbc, $cart_query);	if (mysqli_num_rows($cart_result) > 0)	{	echo "<table>";	echo "<tr><th>Item</th><th>Description</th><th>Qty</th><th class=\"right\">Price</th></tr>";	echo "<form action=\"cart.php\" method=\"post\">";	echo "<input type=\"hidden\" name=\"do\" value=\"update\" />";	$total = 0;		while ($row = mysqli_fetch_array($cart_result, MYSQLI_ASSOC))		{		$price = $row['price'];		$subtotal = $_SESSION['cart'][$row['id']]* $price;		$total += $subtotal;		$subtotal = number_format($subtotal, 2);		echo "<tr><td>{$row['product']}</td><td>{$row['product']}</td><td><input type=\"text\" style=\"width: 2em\" name=\"qty[{$row['id']}]\" value=\"{$_SESSION['cart'][$row['id']]}\" /></td><td class=\"right\">£$price</td></tr>\n";		}		echo "<tr><td colspan=\"3\" class=\"right noBorder\">Total:</td><td class=\"right\">£ " . number_format($total, 2) . "</td></tr>";		echo "<tr><td colspan=\"4\" class=\"noBorder\">Set an items quantity to 0 to remove it from cart.</td></tr>";		echo "</table>";		echo "<div style=\"text-align:center;\"><button type=\"submit\" name=\"submit\" value=\"update\">Update cart</button></div>";		echo "</form>";	}}else{echo "<p>You have not selected any items yet.</p>";}

Each time the cart gets updated or accessed page after you added items to cart before the message 'You have not selected any items yet.' comes up which should only appear when no items had been added to cart before. Where am I going wrong?Son

Link to comment
Share on other sites

If the else part is getting executed, the obvious answer is because the if part is false. You should be able to add some debugging statements earlier up in the code to print out all the variables you're comparing to see why the execution is going where it is.
Will have a go to do some more debugging... Also, I wondered how you would also pass quantity to cart page?Son
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...