padams Posted November 6, 2008 Share Posted November 6, 2008 I've got multiple session variables called $_SESSION['cart'][$productID], with data in each called 'stylename', 'quantity' and 'price'. For example, there will be $_SESSION['cart'][1], $_SESSION['cart'][2], etc. Is there any way to combine data from each into one variable? So I could have "productID1, stylename1, quantity1 and product2, stylename2, quantity2", etc. Quote Link to comment https://forums.phpfreaks.com/topic/131607-imploding-data-from-multiple-sessions/ Share on other sites More sharing options...
JonnoTheDev Posted November 6, 2008 Share Posted November 6, 2008 Using a loop. Not sure why you want this though, data is better in array? for($x = 0; $x < count($_SESSION['cart']); $x++) { ${"product".$x} = implode(",",$_SESSION['cart'][$x]); } print $product1."<br />"; print $product2; Quote Link to comment https://forums.phpfreaks.com/topic/131607-imploding-data-from-multiple-sessions/#findComment-683727 Share on other sites More sharing options...
padams Posted November 6, 2008 Author Share Posted November 6, 2008 I'm just not sure how to best store the information. I've got an array called $_SESSION['cart'][$productID], but whenever a new item is added we need to check if the product has already been purchased so we can increment quantity by 1. That said, the style is a complicating matter as if the product has already been ordered but it is a different style, then it should become a new item in the shopping cart. I just don't know how to best store the information, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/131607-imploding-data-from-multiple-sessions/#findComment-683941 Share on other sites More sharing options...
JonnoTheDev Posted November 6, 2008 Share Posted November 6, 2008 The current session storage method using sessions is fine. In terms of quantities you just need to check if the product Id already exists as an array key. If so increment the quantity key by 1 // the product is added to the cart via a form post $productId = $_POST['productId']; // check if it exists in the cart if(array_key_exists($productId, $_SESSION['cart'])) { // update the quantity $_SESSION['cart'][$productId]['quantity'] = $_SESSION['cart'][$productId]['quantity']+1, } else { // add the new product to the cart } Quote Link to comment https://forums.phpfreaks.com/topic/131607-imploding-data-from-multiple-sessions/#findComment-684058 Share on other sites More sharing options...
padams Posted November 7, 2008 Author Share Posted November 7, 2008 But what about the style information? The quantity shouldn't increase if the style is different, it should create a new session variable. Quote Link to comment https://forums.phpfreaks.com/topic/131607-imploding-data-from-multiple-sessions/#findComment-684262 Share on other sites More sharing options...
JonnoTheDev Posted November 7, 2008 Share Posted November 7, 2008 By style im guessing you mean i.e. colour, size, etc (different product options) If the styles are related to the productId then add the styles in a sub array i.e. $_SESSION['cart'][1] = array('name' => product xyz, 'qty' => 2, styles => array('red' => 1, 'blue' => 1)); So the user has added 2 of product xyz to the cart,1 red and 1 blue. Have you inherited this code from somewhere as the cart model seems sound and the fact you are struggling suggests this. If I were you I would break down each user action into smaller steps and tackle each one i.e. User adds item to cart - 1. Check the product is not already in the cart, if so increment the quantity. 2. Send user to basket page 3. Display cart contents User deletes item - 1. Retrieve product from cart and decrease the quantity. If quantity is 1 remove the product 2. Send user to basket page 3. Display cart contents User logs in - 1. Obtain guest session cart contents and transfer to database storage referenced by the user id If you struggle with arrays or sessions then do some tutorials or reading up. Quote Link to comment https://forums.phpfreaks.com/topic/131607-imploding-data-from-multiple-sessions/#findComment-684419 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.