Jump to content

Reload Page Issue


son
 Share

Recommended Posts

Having finally manage to make my shopping basket page work I found that when I re-load the page all data is lost and the user is confronted with message which comes up when no items have been added to page yet. Why does a reload loose all data? The working code is:

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 the following item has been added to your quote page: $product with a price of £$price.</p>\n";				}				else				{				$_SESSION['cart'][$id] = 1;				echo "<p>The following item has been added to your quote page: $product with a price of £$price.</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>The add to quote page has been updated.</p>";}if ($_SESSION['cart'] && !empty($_SESSION['cart'])){$cart_query = "SELECT product_id, 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 style=\"margin-top:15px;\">";	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['product_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['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_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 add to quote page.</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>Currently there are no items added to this page.</p>";}

Any helps, hints or kicks appreciated;-)Son

Link to comment
Share on other sites

You don't seem to be starting the session anywhere, is that the entire script?
You got me, many thanks. Forgot to include on top of presented script 'session_start();'. Just two more questions: I only have 'session_start();' on actual basket page (which works), but should/can I have it for each page? This always confused me. In addition, do you know on product page how to also pass quantitiy (from form field) with product id 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...