Jump to content

Archived

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

juanc

the basics of a shopping cart

Recommended Posts

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?

Share this post


Link to post
Share on other sites
You could use a session for guest users say but you'd be better off using a database.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 info
if(!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.

Share this post


Link to post
Share on other sites
Hey I have to say thanks especially if you just wrote that for me!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.