Jump to content


Photo

the basics of a shopping cart


  • Please log in to reply
5 replies to this topic

#1 juanc

juanc
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 09 June 2006 - 07:25 PM

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?

#2 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 07:28 PM

You could use a session for guest users say but you'd be better off using a database.
[table]



Don't worry, the printer fairies will sort it out.

#3 juanc

juanc
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 09 June 2006 - 07:42 PM

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?

#4 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 08:06 PM

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:
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.
Hope that helps.
[table]



Don't worry, the printer fairies will sort it out.

#5 juanc

juanc
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 09 June 2006 - 08:26 PM

Hey I have to say thanks especially if you just wrote that for me!

#6 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 08:27 PM

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
[table]



Don't worry, the printer fairies will sort it out.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users