Alexhoward Posted July 1, 2009 Share Posted July 1, 2009 Hi Guys, really stuck with this one and was hoping someone on here could help? been all over google and can't find the answer... I'm using a session to hold products id when added to the shopping basket. problem is i don't know how to tell if they have already been added. on the sales page of the website i loop through all the products in the database, pulling all the relevant details, as well as adding an add to cart button if the product has not been sold. this disapears if the product is sold. as they only sell single items i would also like to exclude the "add to basket" button when the item has been added. Therefore does anyone know a way of checking if the item is already in the session as i loop through to pull the products? hope that makes sense...! thanks in advance! As the site only sells single products Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/ Share on other sites More sharing options...
flyhoney Posted July 1, 2009 Share Posted July 1, 2009 We really need more information. How are you saving the products to the user's session? Can we see the code that saves the product to the user's sessions as well as the code the displays the products? Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867284 Share on other sites More sharing options...
sloth456 Posted July 1, 2009 Share Posted July 1, 2009 I second that, show us some code. This should not be a hard thing to do. Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867287 Share on other sites More sharing options...
Alexhoward Posted July 1, 2009 Author Share Posted July 1, 2009 Hi guys, thanks for the reply ok so i add/remove from the session using a link like: <a href="add_list.php?action=add&id='.$prod_id.'"><img src="images/list.jpg" border="0" alt="Add to Shopping List"></a> as the $prod_id is set before in the while loop pulling all the products. the add/remove code is: <?php session_start(); //add item to shopping list if ($action == "add") { $_SESSION['sh'][$product_id]++; } //remove item else if ($action == "remove") { $_SESSION['sh'][$product_id]--; } //empty baasket else if ($action = "empty") { unset($_SESSION['sh']) ; } //if zero clear if($_SESSION['sh'][$product_id] == 0) { unset($_SESSION['sh'][$product_id]) ; } ?> So what i need to do when pulling all the products, is see if the product ID is in the session. if it is then just leave out the add to basket button. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867304 Share on other sites More sharing options...
flyhoney Posted July 1, 2009 Share Posted July 1, 2009 You just need to check to see if a product ID is already set in the session before creating a link to it. It would look something like this: <?php foreach ($products as $product) { if (!in_array($product['id'], $_SESSION['sh'])) { // print link to product } } Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867309 Share on other sites More sharing options...
Alexhoward Posted July 1, 2009 Author Share Posted July 1, 2009 hi. I have tried many variations of this method. When i use this it either just works on the last item added to the basket. or affects all of the items cross the entire site...? i'll try your version thou in case i was being a noob...!! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867313 Share on other sites More sharing options...
Alexhoward Posted July 1, 2009 Author Share Posted July 1, 2009 I just can't get it to work..!! so i'm adding the items into $_SESSION['sh'] I'm then using a while loop to pull the products from the database so as it loops through each product i'm setting the ID to $prod_id i'd like to say something like: if(in_array($prod_id,$_SESSION['sh'])) { //take the button away..... } does anyone know how to do this...? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867419 Share on other sites More sharing options...
Alexhoward Posted July 2, 2009 Author Share Posted July 2, 2009 anyone....? i really have no idea what to do... I have finished all the work apart from this tiny bit.. please... Cheers Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-867994 Share on other sites More sharing options...
flyhoney Posted July 3, 2009 Share Posted July 3, 2009 do a print_r of $_SESSION['sh']. I want to see it's structure. Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-868421 Share on other sites More sharing options...
Alexhoward Posted July 4, 2009 Author Share Posted July 4, 2009 Hiya, I have added three products to the list: the shopping list contains product id and then quantity. i ripped this code off another site i made and so don't need quantity, but leaving it there shouldn't matter... should it...? :-\ print_r($_SESSION['sh']) ; Array ( [93] => 1 [94] => 1 [95] => 1 ) cheers! Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-868883 Share on other sites More sharing options...
Alexhoward Posted July 5, 2009 Author Share Posted July 5, 2009 Hi Guys! i've fixed it, but in a round-about way... firstly i created a session for the product id when adding to the basket e.g. $_SESSION['id'.$product_id] ; i then loop through the products looking to see if the session for the product_id has been set. if not it's not in the basket. when emptying the basket i simple clear all session as i'm not holding any customer login info. Thanks for all the help, and i'm sure there's a better way, but this works! hope that all made sense, Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/164419-solved-how-to-see-if-product-is-already-in-basket-session-soo-stuck/#findComment-869218 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.