Jump to content

the basics of a shopping cart


juanc

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.