Jump to content

adding items to sessions and retieving results


ragrim

Recommended Posts

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

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 );
?>

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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.