...

Shopping cart development with sessions

bcarl314
07-20-2002, 05:00 AM
All right, I can't figure out the best way to do this.

I want to create a shopping cart on my site which will allow users to add multiple items to their cart. Sounds easy enough. I've done it client side, but now am "expanding my horizons" with server side.

What I would like to do is register a session array named items, which will contain the SKUs of the product ordered.

So every time someone "adds" an item to their cart it pushes this array and maintains the data throughout the session. Only problem is I can't get that to work.

I've tried the following:

session_start();
session_register('items[]');
array_push($items,$sku);

but I get an items not defined error.

Is this how you would go about it? Is there a better way? Why doesn't the method above work?

Spookster
07-20-2002, 04:46 PM
It would probably be better to create a shopping cart class. Within that class you can writen any methods needed such as adding items, deleting items, showing items, etc. Then you would simply instantiate that class first and then register it with that session. For example:



class MyShoppingCart{

var $items = array();

function add_item($sku) {
if ($this->items[$sku]){

$this->items[$sku] = $this->items[$sku] + 1;
}
else {
$this->items[$sku] = 1;
}
}

function delete_item($sku) {
$this->items[$sku] = 0;
}

}



Then when they want to use the cart:

$cart = new MyShoppingCart();
session_register("cart");


oila!! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum