Freedom-n-Democrazy Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/ Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276769 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 Shit. Looks like I am going to have to re-write my code to assign array keys to each object that is added into the session, right? Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276789 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276792 Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276793 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 An object in a $_SESSION. Buddski, maybe an array key can be created artificially on the way into the session? using a randomized number and associating it with the object in the foreach loop? Pretty sloppy I know, but hey it might work and save a re-write? Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276795 Share on other sites More sharing options...
ManiacDan Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276798 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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); Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276800 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 Thanks ManiacDan, I know what your saying but I want to use objects and have my cart show two of the same item if someone buys two, rather 2 x Item. Why? Because it mimics a real shopping cart. lol Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276801 Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 Thanks ManiacDan, but I want to use objects. do you even know what an object is? Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276802 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276805 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276806 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276807 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 I have a piece of code here that will change the entire way you store the data in the session. Let me know if you want to see it Dont want to post it if it will just confuse/annoy you. Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276809 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 LoL hold up, I got a vision of a flow in my head. I wanna see how good I am. If I totally fail, then yeah I'll defiantly wanna check out your flow. Maybe keep that code close by LOL! I'll be done in about 20 I'm just gonna write it up now. Man why is it always the last thing that brakes ya balls! :S Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276814 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276823 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 If somebody does hit refresh from a POSTed form they will be asked if they want to resend the POST data, yes will re post no will usually show up blank. Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276825 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276867 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276874 Share on other sites More sharing options...
ManiacDan Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276887 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276896 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276901 Share on other sites More sharing options...
Andy-H Posted October 7, 2011 Share Posted October 7, 2011 unset($_SESSION['cart']['content']); Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276902 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 7, 2011 Author Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276916 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Try $_SESSION['cart']['content'][$item_found]['quantity'] = $_SESSION['cart']['content'][$item_found]['quantity'] + $quantity; Link to comment https://forums.phpfreaks.com/topic/248624-remvoing-an-object-from-_session/#findComment-1276920 Share on other sites More sharing options...
Recommended Posts