Jump to content

SESSION variables...again


mr_jim

Recommended Posts

Hey, posted something a few days, thought I had fixed it but how little I know. Anyway I am storing information about a shopping basket in a session array. It seemed to work fine on my local machine on apache, but as soon as it was loaded to the server they seem to behave very irratically i.e disappear/reappear when deleted or quantites change. Has anything similar happened to anyone else. Help would be appreciated. Code is below: -

 

Cheers

 

Jim

 

<?php

ob_start();
session_start();

if(isset($pagetitle)) {
$pagetitle = 'Tam & Rob';
}

?>

<!DOCTYPE html PUBLIC "-//W3c//DTD XHTML 1.0 Transitional//EN"
"http://www.w3c.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.wc3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html ; charset=iso-8859-1" />
<title>Tam & Rob</title>
<link href="includes/styles.css" rel="stylesheet" type="text/css">
</head>
<!-- End of header -->

<?php

echo '<body>';

include ('includes/menu.html');

//check adding to cart
if (is_numeric ($_POST['pid'])) {
$pid= $_POST['pid'];
$colour = $_POST['colour'];
$size = $_POST['size'];

	//Create cartitem ID
	if (isset ($_SESSION['cart'])) {
		foreach ($_SESSION['cart'] as $key => $value) {
		if (isset($value)) {

		//new code to make sure key isnt blank
		if ($key == 0){
			$key = 1;
		}

			$i = $key;
		}
		              $cart_item = $i + 1;
		}
	} else {
		$cart_item = 1;
	}

//Check if product exists
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
 if ($_SESSION['cart'][$key]['pid'] == $pid){
	if ($_SESSION['cart'][$key]['size'] == $size){
	      if ($_SESSION['cart'][$key]['colour'] == $colour){
	      //product does exist so add to quantity
		$cart_item = $key;
		     if (isset ($_SESSION['cart'][$cart_item]['qty'])){
			$qty = $_SESSION['cart'][$cart_item]['qty'] + 1;
		      }else{
			$qty = 1;
		      }
	      }
}	
          }
}

} else {
$qty = 1;
}

	if (!isset ($qty)){
		$qty = 1;
	}
if (!isset($cart_item)){
	$cart_item = 1;
}

$_SESSION['cart'][$cart_item]['qty']=$qty;
$_SESSION['cart'][$cart_item]['pid']=$pid;
$_SESSION['cart'][$cart_item]['size']=$size;
$_SESSION['cart'][$cart_item]['colour']=$colour;	
}

//post script which changes value
if (isset ($_POST['submit'])) {

foreach ($_SESSION['cart'] as $upkey => $upvalue) {
$qtyup = $_POST['qty'][$upkey];
if (($qtyup == 0) AND (is_numeric ($qtyup))) {
	unset ($_SESSION['cart'][$upkey]);
} elseif (is_numeric($qtyup) AND ($qtyup > 0) ) {
$_SESSION['cart'][$upkey]['qty'] = $qtyup;
}

}
}

//post empty script which empty's cart
if (isset ($_POST['empty'])) {
unset ($_SESSION['cart']);
}

//Check if empty
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
	if (isset($value)) {
		$empty = FALSE;
	}
}
}


//Cart not empty
if (!$empty) {

require_once ('includes/config.inc');

require_once('includes/mysql_connect.php');

//Retrieve information
$query = 'SELECT * FROM garments WHERE product_id IN (';
foreach ($_SESSION['cart'] as $key => $value) {
	$query .= $_SESSION['cart'][$key]['pid'] . ',';
}

$query = substr ($query, 0, -1) . ') ORDER BY style';
$result = mysql_query($query);

//Create table & form
echo '<table border="0" width="700px" cellspacing="3" cellpadding="3">
<tr>
	<td align="left" width="30%"><b><u>Product Name</u></b></td>
	<td align="left" width="30%"><b><u>Colour</u></b></td>
	<td align="left" width="10%"><b><u>Size</u></b></td>
	<td align="left" width="10%"><b><u>Price</u></b></td>
	<td align="left" width="10%"><b><u>Qty</u></b></td>
	<td align="left" width="10%"><b><u>Total Price</u></b></td>
</tr>
<form action="view_cart.php" method="POST">
';

//Print each item
$total = 0; //Total Cost of the order
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

	//Calculate the total and subtotals.
	foreach ($_SESSION['cart'] as $key => $value) {
	if ($_SESSION['cart'][$key]['pid'] == $row['product_id']){

	$subtotal = $_SESSION['cart'][$key]['qty'] * $row['price'];
	$total += $subtotal;

	echo " <tr>
	<td align=\"left\">{$row['style']}</td>
	<td align=\"left\">{$_SESSION['cart'][$key]['colour']}</td>
	<td align=\"left\">{$_SESSION['cart'][$key]['size']}</td>
	<td align=\"left\">{$row['price']}</td>
	<td align=\"left\"><input type=\"text\" size=\"3\" name=\"qty[$key] \" 
	value=\"{$_SESSION['cart'][$key]['qty']}\" /></td>
	<td align=\"left\">£" . number_format($subtotal, 2) . "</td></tr>\n";
		}
	}


} //end while

//Close table
echo " <tr>
<td colspan=\"5\" align=\"right\"><b>Total:</b></td>
<td align=\"left\">£" . number_format($total, 2) . "</td>\n
</tr>
</table><div><input type=\"submit\" name=\"submit\" value=\"Update My Cart\"/>\n
<input type=\"submit\" name=\"empty\" value=\"Empty Cart\"/></form>\n
<form action=\"checkout.php\" method=\"POST\">
<input type=\"hidden\" name=\"total\" value=" . number_format($total, 2) .">
<input type=\"submit\" name=\"submit\" value=\"Checkout\"></form></div>";
mysql_free_result($result);
mysql_close();
?>

<?php
} else {
echo '<p>Your cart is currently empty.</p>';
}

include_once ('includes/footer.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.