raptor30506090 Posted July 3, 2012 Share Posted July 3, 2012 Hi guys how would i get this in to a session <?php $products = array(array( 'Code' => 'TIR', 'Description' => 'Tires', 'Price' => 10, 'Name' => 'Darren' ), ); for($row = 0; $row < 1; $row++){ while(list($key, $value) = each($products[$row])){ echo $value; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265143-session/ Share on other sites More sharing options...
raptor30506090 Posted July 3, 2012 Author Share Posted July 3, 2012 Guy im still stuck what im trying to do is 1: create a shopping cart option and session['cart'] 2: options like a shed cost ?500 size 10x10 then adds an option to have the front made in bars and then also orders the same with out the option of bars it has the same id so in my session it over rights can any one tell me the right way to go about it or spell it out to me. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/265143-session/#findComment-1358786 Share on other sites More sharing options...
ManiacDan Posted July 3, 2012 Share Posted July 3, 2012 Didn't we already do this in your last thread? What's the problem? Where are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/265143-session/#findComment-1358795 Share on other sites More sharing options...
raptor30506090 Posted July 3, 2012 Author Share Posted July 3, 2012 So i now im a pain I had a look at what you said and still couldnt work it out as you can tell im new at carts what im trying to do is in the products have options with price changes but when i do this it over rights the last one in the session with same id so if you order same type of shed say 10x10 at $500 then they what same type again but 10x20 at $750 i would like a new product in the cart but i keep over righting last session Many thanks Daz Quote Link to comment https://forums.phpfreaks.com/topic/265143-session/#findComment-1358797 Share on other sites More sharing options...
ManiacDan Posted July 3, 2012 Share Posted July 3, 2012 Like this. This is exactly what I suggested, it was 20 lines counting comments: <?php function addToCart ( $productId, $quantity, $size, $color, $price ) { //if the productID doesn't exist, just create it: if ( !isset($_SESSION['cart'][$productId]) ) { $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price)); return; } //product already exists, check to see if it's the same as another in the group: foreach ( $_SESSION['cart'][$productId] as &$product ) { //NOTE: If you want to override pricing with new tiers, you can remove 'price' from this comparison. You may also want to // fetch prices from the database when you DISPLAY the cart if you do bulk discounts if ( $product['price'] == $price && $product['color'] == $color && $product['size'] == $size ) { $product['quantity'] += $quantity; return; } } //if we've reached here, the productID is already in the cart but the current item is not a duplicate of an existing one: $_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price); } //TEST CODE //set up the cart: $_SESSION['cart'] = array( '123' => array(array('quantity' => 2, 'size' => '10x10', 'color' => 'blue', 'price' => '899.95')), '456' => array(array('quantity' => 1, 'size' => 'Large', 'color' => 'green', 'price' => '9.95'), array('quantity' => 3, 'size' => 'Small', 'color' => 'black', 'price' => '8.99')), ); echo "<pre>\n\n"; print_r($_SESSION['cart']);echo "\n\n"; //add a new green large 456: addToCart('456', 1, 'Large', 'green', 9.95); print_r($_SESSION['cart']);echo "\n\n"; //add three different sized 123 products: addToCart('123', 3, '5x5', 'blue', 499.95); print_r($_SESSION['cart']);echo "\n\n"; //add a completely new product offering: addToCart('789', 30, 'Refill', 'yellow', .90); print_r($_SESSION['cart']);echo "\n\n"; Quote Link to comment https://forums.phpfreaks.com/topic/265143-session/#findComment-1358869 Share on other sites More sharing options...
raptor30506090 Posted July 3, 2012 Author Share Posted July 3, 2012 Thank you for that im going to have a go now. cheers Quote Link to comment https://forums.phpfreaks.com/topic/265143-session/#findComment-1358930 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.