ragrim Posted November 21, 2011 Share Posted November 21, 2011 Hi, Im trying to learn how to build a shopping cart system, im have a few problems and im not sure where im going wrong, it looks as though im getting an item into the session, but it looks like when i add another it over writing the first one, and im having trouble trying to echo it out. Here is what i have atm session_start(); include('functions.php'); $pid = $_GET['product']; $q = $_GET['qty']; if($_GET['function']=="add") { echo "add " . $_GET['product']; $_SESSION['cart']=array(); $_SESSION['cart']['productid']=$pid; $_SESSION['cart']['qty']=$q; } print_r($_SESSION['cart']); echo showCart(); Am i way off on my code? regardless of how many things i try to add to session, all i can print out is the last one i entered. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/251514-adding-items-to-sessions-and-retieving-results/ Share on other sites More sharing options...
creata.physics Posted November 21, 2011 Share Posted November 21, 2011 You have the right idea, the issue is with your array. Trying changing this bit here: <?php $_SESSION['cart']=array(); $_SESSION['cart']['productid']=$pid; $_SESSION['cart']['qty']=$q; ?> To this bit here: <?php $_SESSION['cart'][] = array('productid' => $pid, 'qty' => $q ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251514-adding-items-to-sessions-and-retieving-results/#findComment-1289909 Share on other sites More sharing options...
Drummin Posted November 21, 2011 Share Posted November 21, 2011 First I would change your code over to use $_POST. Check to see if your session is set and if not then start your array, otherwise you are re-writing the array as empty. Personally, I would just save the product id as the array key and the quantity as the array value. I would then see if the product is in the "cart" already and if so, get the quantity and add the new quantity to it. Here's an example. <?PHP session_start(); include('functions.php'); IF (!isset($_SESSION['cart'])){ $_SESSION['cart']=array(); } ///////////////////////////// //CHANGE TO $_POST values!!!! //But for testing I will leave as get. //////////////////////////// $pid = $_GET['product']; $q = $_GET['qty']; if($_GET['function']=="add"){ if(array_key_exists($pid, $_SESSION['cart'])){ $_SESSION['cart'][$pid]=$_SESSION['cart'][$pid]+$q; } ELSE{ $_SESSION['cart'][$pid]=$q; } } print_r($_SESSION['cart']); ?> EDIT: I see another example was posted. Keep in mind that array keys should be unique and using "productid" as a key will not work for adding multiple items to the cart. That's why I like to use the product id as the key. Quote Link to comment https://forums.phpfreaks.com/topic/251514-adding-items-to-sessions-and-retieving-results/#findComment-1289911 Share on other sites More sharing options...
ragrim Posted November 28, 2011 Author Share Posted November 28, 2011 Thanks for that example it worked a charm, but im having problems trying to get the details out of it. ive added this code $cart = $_SESSION['cart']; foreach ($cart as $id) { echo $id . "<br>"; } This echos out the quantities for each of the items but i cant seem to be able to get the pid out, im assuming its because the pid has been saved as the array id? so question is how can i get that out in a way where i can query my DB for each of the PID's cheers Quote Link to comment https://forums.phpfreaks.com/topic/251514-adding-items-to-sessions-and-retieving-results/#findComment-1291752 Share on other sites More sharing options...
Drummin Posted November 28, 2011 Share Posted November 28, 2011 The code I posted is saving item id as key and quantity as value. You'll do something like this. $cart = $_SESSION['cart']; foreach ($cart as $id => $quantity) { echo "ID:". $id . "Quantity:". $quantity. "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/251514-adding-items-to-sessions-and-retieving-results/#findComment-1291870 Share on other sites More sharing options...
ragrim Posted November 28, 2011 Author Share Posted November 28, 2011 Thanks for the reply i managed to figure it out from a few tutorials i found, all is going well. Now i need to figure out how i can update and delete items from the session but ill have a crack at doing it myself before i ask for help. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/251514-adding-items-to-sessions-and-retieving-results/#findComment-1291998 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.