juanc Posted June 9, 2006 Share Posted June 9, 2006 I'm trying to work out the basic mechanics of a shopping cart.......specifically how do the items selected get displayed in a temporary staging area......Am I right in saying that you can keep appending items onto a session variable...then do an explode or foreach on it. If so can anyone give me any sample code to get me started and also is this the typical approach? Quote Link to comment https://forums.phpfreaks.com/topic/11604-the-basics-of-a-shopping-cart/ Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 You could use a session for guest users say but you'd be better off using a database. Quote Link to comment https://forums.phpfreaks.com/topic/11604-the-basics-of-a-shopping-cart/#findComment-43796 Share on other sites More sharing options...
juanc Posted June 9, 2006 Author Share Posted June 9, 2006 Thanks for the reply........I thought about using a database table but the problem with that would be where customers don't go through to the check out ............I'd end up having rows that don't serve any purpose.Even so I was generally asking can a session variable keep having values appended to it and is that the typical approach? Quote Link to comment https://forums.phpfreaks.com/topic/11604-the-basics-of-a-shopping-cart/#findComment-43803 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 If you use a database to store the values of registered users and whatever they've currently got in their cart, you won't have any unused rows. As for the session, you can append values if you want but it'd be better to use an array like:[code]session_start();// When a user visits your page they are given temporary infoif(!isset($_SESSION)){$_SESSION['items'] = array();}// Then when they add an item to their basket you just do$_SESSION['items'][] = $item_id;// You could then find the quantities by counting the number of equal product ids// or you could extend the array futher and use various functions to add and// subtract values from the quantity depending on what the user does// Set up sessions like this instead$_SESSION['items'][$i]['id'] = '';$_SESSION['items'][$i]['qty'] = 0;function add_item($item_id){$items = $_SESSION['items'];$flag_found = false;foreach($items as $key => $item){if($item['id'] == $item_id){$flag_found = $key;}}if(!$flag_found){$cnt = count($_SESSION['items']);$_SESSION['items'][$cnt]['id'] = $item_id;$_SESSION['items'][$cnt]['qty'] = 1;}else{$_SESSION['items'][$flag_found']['qty']++;}}// You'd do something similar for item_remove etc.[/code]Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/11604-the-basics-of-a-shopping-cart/#findComment-43818 Share on other sites More sharing options...
juanc Posted June 9, 2006 Author Share Posted June 9, 2006 Hey I have to say thanks especially if you just wrote that for me! Quote Link to comment https://forums.phpfreaks.com/topic/11604-the-basics-of-a-shopping-cart/#findComment-43822 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 Yup, it's not copied from anywhere. But remember it's just various examples so you can't just run that code. For instance $_SESSION['items'][$i]['id'] = ''; won't work straight off. $i would have to be defined first. But the function'll work fine and you hopefully get the idea Quote Link to comment https://forums.phpfreaks.com/topic/11604-the-basics-of-a-shopping-cart/#findComment-43824 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.