Jump to content

session variable problem


nvanilkumar

Recommended Posts

page.php

<a href="cart.php?action=add&id=38">

 

cart.php

 

session_start();

$cart = $_SESSION['cart'];

 

$action = $_GET['action'];

 

switch ($action) {

case 'add':

if ($cart)

$cart =$cart. ','.$_GET['id'];

else

$cart = $_GET['id'];

}

 

 

$_SESSION['cart'] = $cart;

echo $cart;

output:

Insted of one time it adds the id two times.

 

It prints : 38,38.

can pls suggest me what's problem in the code. Thank's in advance.

 

Link to comment
Share on other sites

I suggest storing the results in an array rather than trying to delimit them with commas. Try this script out

 

<?php 

session_start();

// Check if the user wants to clear the cart, or if a cart doesn't exist yet
if( isset($_GET['empty']) || !isset($_SESSION['cart']) )
$_SESSION['cart'] = array();

// Check if a user has added an item
if( isset($_POST['quantity']) && isset($_POST['id']) ) {
// This will make possibly things easier to follow
$id = $_POST['id'];
$quantity = $_POST['quantity'];
// &$var is assign by reference. Any changes to $cart will apply to $_SESSION['cart'].
// It's essentially creating two ways to access the same variable.
$cart = &$_SESSION['cart'];
// We will store the item and quantity in the array. $array[item] = quantity
// Check if the item is already in the cart
if( isset($cart[$id]) )
	// If so, add quantity to it. (int) forces $id to be returned as an integer.
	$cart[$id] += (int) $quantity;
else
	// Otherwise, set it to the quantity
	$cart[$id] = (int) $quantity;
// Destroy the reference to $cart to avoid acidentally screwing with it elsewhere in the script
unset( $cart );
}

// Show the items in the cart
echo '<div><h3>Items in cart:</h3><ul>';
// Check if there are no items in cart
if( empty($_SESSION['cart']) )
echo '<li style="font-style: italic;">Empty</li>';
// Otherwise, loop through
else {
foreach( $_SESSION['cart'] as $id => $quantity )
	echo "<li>$quantity x of $id</li>";
}
// End items in the cart
echo '</ul></div>';

// Basic forms to add to cart
?>
<div>
<h3>Add products to cart</h3>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
	Item number: FOO-001 <input type="text" name="quantity" size="5" value="0">
	<input type="hidden" name="id" value="FOO-001">
	<input type="submit" value="Add">
</form>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
	Item number: FOO-052 <input type="text" name="quantity" size="5" value="0">
	<input type="hidden" name="id" value="FOO-052">
	<input type="submit" value="Add">
</form>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
	Item number: BAR-717 <input type="text" name="quantity" size="5" value="0">
	<input type="hidden" name="id" value="BAR-717">
	<input type="submit" value="Add">
</form>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
	Item number: FOO-102 <input type="text" name="quantity" size="5" value="0">
	<input type="hidden" name="id" value="FOO-102">
	<input type="submit" value="Add">
</form>
</div>
<a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?empty=1">Empty the Cart</a>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.