Jump to content

Remvoing an object from $_SESSION


Recommended Posts

I have a $_SESSION that I add to with these peices of code:

 

if (isset($_GET['action']) && isset($_GET['size']) && $_GET['action'] == 'add') {
$id = intval($_GET['id']);
$size = $_GET['size'];
$_SESSION['cart']['content'][] = array ('size' => $size, 'id' => $id);
}

<A href="index.html?action=add&id=1&size=sizel">Add to cart</A>

 

I was wondering if theres a way to reverse this action? or must the cart be reset?

Link to comment
Share on other sites

You can delete an element from the array, but with the code you have already, you will have to pass in the array key of the element you want to delete.

If somebody buys 2 of the one product (same size) in your current code it will create 2 SESSION elements and the only way to differentiate between them is the array key.

if (isset($_GET['delete'])) {
unset($_SESSION['cart'][$_GET['delete']);
}
//used with
<a href="index.html?delete=array_key_of_the_element_to_delete">Delete from Cart</a>

Link to comment
Share on other sites

Actually, MAYBE not!

I would think there has to be array keys already. Is this a way to find out an array key for a session object? I've got an awesome idea that might save me from depression. LOLLL

is what a way to find a session variable key?

Link to comment
Share on other sites

You'll have to loop through the array to find the matches.

 

The proper way to do a shopping cart is to store the item ID as the key, and put the QUANTITY on the object, so if someone buys an item you just do:

$itemTheyWant = 123;
$quantityTheyWant = 3;
if ( isset($_SESSION['cart'][$itemTheyWant]) ) {
  $_SESSION['cart'][$itemTheyWant]['quantity'] += $quantityTheyWant;
} else {
  $_SESSION['cart'][$itemTheyWant] = array('itemId' => $itemTheyWant, 'quantity' => $quantityTheyWant);
}

Modified to fit your structure of course, I know you're using objects.

 

-Dan

Link to comment
Share on other sites

I was thinking of trying this. What do you think?

 

if (isset($_GET['action']) && isset($_GET['size']) && $_GET['action'] == 'add') {
$id = intval($_GET['id']);
$size = $_GET['size'];
$cartkey = rand (1, 1000000000)
$_SESSION['cart']['content'][] = array ('size' => $size, 'id' => $id, $cartkey => $cartkey);

 

Didn't work as expected, its assigning the random number to all the same objects with the sam.e... hmmm hang on a second, each time I add something with the same name, they are not registering as two objects. Why?

Link to comment
Share on other sites

I would suggest implementing ManiacDans suggestion with one small detail (because I know the rest of your code :))

We can agree to make the product id the key, but whilst checking if the element is in the SESSION, you should also check the size part also, and only add the qty IF the size is the same. Otherwise they should be treated as separate entries (which will need something added to the product id like _2), unless of course each size has its own qty count inside the product (but thats a whole other kettle of fish)

Link to comment
Share on other sites

I'll be honest, 60% of the reason I didn't wanna role with ManiacDans suggestion is because I know I can do it, and I also know doing it means not going out for the rest of the night as I am going to have to change the flow in how things get added to cart from .html?thisway=bla, to a form... and its such as awesome night. Ok, I will do it. I'm gonna start getting to work on it now. Bloody hell what I am doing with my life. :S

Link to comment
Share on other sites

I will be a little longer.

 

I've found using HTML POST a MUCH better way to add to a cart. Its cleaner, more relaxing on my mind to code, and it doesn't have the problem where if someone hits refresh, it will add another item to the cart, where as .html?add=bla" did.

Well, this does actually, but its less likely to occur.

 

It also shows me that SESSIONS are not as hard as I thought. :)

Link to comment
Share on other sites

haha yeah, thats what I meant, but its gonna be a little harder, but now the page that they see directly after adding just says "Product added to cart" (I'll change this later on to be more specific which item, size.. you know. I couldn't care for cosmetics now)

 

Well, I re-engineered my flow and it worked, PERFECTLY, no hick ups what so ever (once it was implemented).... I cannot remember a time since the start of my 1.5 month PHP journey that this has happened.

What I've done is, got my page to forward the data by FORM like this:

 

<FORM action="../../../../cart/addtocart.html" method="post" name="form">
<DIV>
	Add <TEXTAREA class="quantity" cols="0" name="quantity" rows="0"></TEXTAREA>
	 <SELECT class="size" name="size"><OPTION>Large</OPTION></SELECT> to 
	<INPUT class="cart" type="submit" value="CART">
	<INPUT class="id" name="id" type="hidden" value="<?php echo $id; ?>">
</DIV>
</FORM>

 

... and then addtocart.html:

<?php
$quantity = $_POST['quantity'];
$size = $_POST['size'];
$id = $_POST['id'];
$_SESSION['cart']['content'][] = array ('quantity' => $quantity, 'size' => $size, 'id' => $id);
echo "Product added to cart.";
?>

 

Now here comes a hick up. Quantity just stays as "1", so I tried this (note the +=>):

$_SESSION['cart']['content'][] = array ('quantity' +=> $quantity, 'size' => $size, 'id' => $id);

... but nup! Any suggestions? I'm gonna read over what ManiacDan said earlier in the mean time.

Link to comment
Share on other sites

You will need to see if the product they are adding to the session already exists.

If they are you need to ADD the entered quantity to it, otherwise add a new entry.

$quantity = $_POST['quantity'];
$size = $_POST['size'];
$id = $_POST['id'];
foreach ($_SESSION['cart']['content'] as $key=>$product) {
if ($product['id'] == $id) {
	$_SESSION['cart']['content']['quantity'] += $quantity;
	echo 'updated item';
} else {
	$_SESSION['cart']['content'][] = array ('quantity' => $quantity, 'size' => $size, 'id' => $id);
	echo 'Added item!';
}
}

Link to comment
Share on other sites

Buddski's solution is similar to my original one except that it doesn't use the product ID as the key (which makes the loop necessary).

 

Also note that nobody, not even you, has used objects.

 

Nobody does the "one line for every product" thing, not even companies that take orders over the phone.  Look at the receipt next time you go to a diner.  The waitress doesn't write:

Coffee

Coffee

Coffee

 

She writes:

Coffee x 3

 

Use the quantity, people don't want to see six copies of the same thing in their cart.  What if they want to remove them?  You make them click 6 times.  What if they wanted 12 instead?  They have to go back and click "add to cart" instead of editing the "quantity" box in the cart.  Keeping the quantity on the row is the proper solution.

 

-Dan

Link to comment
Share on other sites

I just realized that my code is inherently floored, that would keep adding new entries into the session until it found what it was looking for.. this is better.

$quantity = $_POST['quantity'];
$size = $_POST['size'];
$id = $_POST['id'];

$item_found = false;
foreach ($_SESSION['cart']['content'] as $key=>$product) {
if ($product['id'] == $id) {
	$item_found = $key;
	break;
}
}
if ($item_found !== false) {
$_SESSION['cart']['content'][$item_found]['quantity'] += $quantity;
echo 'updated item';
} else {
$_SESSION['cart']['content'][] = array ('quantity' => $quantity, 'size' => $size, 'id' => $id);
echo 'added item'
}

Link to comment
Share on other sites

I just realized that my code is inherently floored, that would keep adding new entries into the session until it found what it was looking for.. this is better.

 

That is exactly what it was doing. I checked over the code 3 times, the third time I walked out to the back yard and back because I thought I was losing it LOL!

I'll check out how the correction behaves.

Link to comment
Share on other sites

The code has a fault where it does not update a product.

It does however know when a product exists (by saying it updated it) or if it doesn't exist at all (by saying it added it).

My eyes are almost shut, but I'm gonna read over and try and see if I can spot anything abnormal.

 

Man, I can't wait to get up tomorrow fresh and read over this code. I've never seen anything like this before. I'm excited to see how it works. For example break; - this sounds interesting... I'll check it out in the manual tomorrow.

Link to comment
Share on other sites

Guest
This topic is now 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.