Version: 0.5

Type: Class

Category: Shopping Carts

License: GNU General Public License

Description: This class provides methods for add, remove, update and remove all items from a basket. The basket works with a session cookie, saving the product ID and quantity in an array for simple accesing it. There's an example in this category... called "Basket example using basket class"



<?php
/*
Basket class for e-commerce purpose

Version : 0.5
Type:Class
Category:Shopping Carts
License: GNU General Public License

Description: This class provides methods for add, remove, update and remove all items
from a basket. The basket works with a session cookie, saving the product ID
and quantity in an array for simple accesing it.
There's an example in this category... called "Basket example using basket class"
*/

class basket {
	var $items;
	var $empty;

	function basket()
	{
		global $cart;
		if(isset($cart))
		{
			$this->items=unserialize(stripslashes($cart));
			if ( gettype($this->items) == "array" )
			{
				$this->empty=false;
			}
			else
				$this->empty=true;
		}
		else
			$this->empty=true;
}

function additem($id, $name, $count, $prix)
{
	if ($this->items[$id][1] > 0 )
	{
		$this->items[$id][1]+=$count;
	}
	else
	{
		$this->items[$id][0]=$name;
		$this->items[$id][1]=$count;
		$this->items[$id][2]=$prix;
		$this->items[$id][3]=stripslashes($name);
	}
	setcookie("cart",serialize($this->items),0,"/");
	$this->empty=false;
}

function removeitem($id) {
	$tmp="";	// Don't forget to initialize !
	if(!$this->empty)  {
		while(list($x,$y)=each($this->items)){
			if($x!=$id) $tmp[$x]=$y;
		};
	};
	$this->items=$tmp;	// Or this will have no effect !
	setcookie("cart",serialize($this->items),0,"/");
	if(count($this->items)==0) $this->empty=true;
}

function resetArray($basket)
{
	reset($basket->items);
}

function countItems($basket)
{
	if(!$basket->empty)
	{
		while ( list($x,$y,) = each($basket->items) )
		{
			$ant++;
		}
	}
	return $ant;
}
function sumItems($basket)
{
	if(!$basket->empty)
	{
		while ( list($x,$y,) = each($basket->items) )
		{
			$ant = $ant + $y[1];
		}
	}
	return $ant;
}
function printItems($b)
{
	if(!$b->empty)
	{
		while ( list($x,$y,) = each($b->items) )
		{
			echo "$x     ".$y[0]."   ".$y[1]."  <a href=\"$PHP_SELF?action=R&id=$x\">Remove</a><br>";
		}
	}
}
function updateitem($id,$count){
	$this->items[$id][1]=$count;
	setcookie("cart",serialize($this->items),0,"/");
}

function removeall(){
	setcookie("cart",NULL,0,"/");
	$this->empty=true;
}

}
?>