Jump to content

SESSION variables for a newbie


mr_jim

Recommended Posts

Hi really struggling to get my head round session variables probably very easy for many of you out there.

 

I am try to store four values against shopping cart. I want to store the product_id, the size, the colour and the qty ordered.

 

Are session variables the best option?

 

currently I have a dynamic loop that stores the product_id ($_SESSION[$product_id]['qty']) as the name of the variable and then stores the quantity against it, but this is no good as the colour and size get replaced everytime.

 

Help would be greatly appreciated.

Link to comment
Share on other sites

Hi,

 

I will post the code below. I wanted to store product_id, colour, size and qty dynamically so I could loop around and populate the shopping basket. Is this possible? Or should I create a mysql table to store the shopping basket information temporarily.

 


<?php
if (is_numeric ($_POST['pid'])) {
$pid= $_POST['pid'];
$colour = $_POST['colour'];
$size = $_POST['size'];

//Check cart already contains one of these products
if (isset ($_SESSION['cart'][$pid]['qty'])) {
	$qty = $_SESSION['cart'][$pid]['qty'] + 1;
} else {
	$qty = 1;
}

//Add to the cart session variable
$_SESSION['cart'][$pid]['qty'] = $qty;
$_SESSION['cart'][$pid]['colour'] = $colour;
$_SESSION['cart'][$pid]['size'] = $size;	

}

//Check if the form has been submitted (to update cart).
if (isset ($_POST['submit'])) {
foreach ($_POST['qty'] as $key => $value) {
	if (($value == 0) AND (is_numeric ($value))) {
		unset ($_SESSION['cart'][$key]);
	} elseif (is_numeric($value) AND ($value > 0) ) {
		$_SESSION['cart'][$key]['qty'] = $value;
	}
}
}

//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 .= $key . ',';
}

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

$result = mysql_query($query);

//Create table & form
echo '<table border="0" width="100%" 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.
	$subtotal = $_SESSION['cart'][$row['product_id']]['qty'] * $row['price'];
	$total += $subtotal;

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

} //end while

//Close table
echo " <tr>
	<td colspan=\"3\" 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\"/></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_close();
?>

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

include_once ('includes/footer.html');

?>

 

Thanks again for the help.

Link to comment
Share on other sites

Sorry forgot to include that I am starting the session. The variables work. But it is just my understanding of how to place them in an array that will allow me to loop around each product and display the qty, size and colour. At present I am storing by the Product_id, but each prodduct can appear more than once example:

 

prodocut_id  product      qty      colour

  1                t-shirt        1        red

  1                t-shirt        1        blue

 

At the moment the colour is replaced evertime as I am sotring it against the product_id. Is there a better way to store it? Forgive me if I am rambling have been trying to solve this for a good few hours. Thanks for your time.

Link to comment
Share on other sites

all you need to do is sort the product_id column out for example.

 

prodocut_id  product      qty      colour

  00001        t-shirt        1        red

  00002        t-shirt        1        blue

 

00001  <<< any t-shirt is red 

00002  <<< any t-shirt is blue

 

 

also where the size is those t-shirts one size fit's all

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.