Location: PHPKode > scripts > shopcart > shopcart/includes/classes/Cart.php
<?php
class Cart {
	var $total = 0;
	var $itemcount = 0;
	var $items = array();
        var $itemprices = array();
	var $itemqtys = array();
        var $iteminfo = array();

	function Cart() {} // constructor function

	function Get_Cart_Contents()
	{ // gets cart contents
		$items = array();
		foreach($this->items as $tmp_item)
		{
		        $item = FALSE;

			$item['id'] = $tmp_item;
                        $item['qty'] = $this->itemqtys[$tmp_item];
			$item['price'] = $this->itemprices[$tmp_item];
			$item['info'] = $this->iteminfo[$tmp_item];
			$item['subtotal'] = $item['qty'] * $item['price'];
                        $items[] = $item;
		}
		return $items;
	} // end of Get_Cart_Contents


	function Add_Cart_Item($itemid,$qty=1,$price = FALSE, $info = FALSE)
	{ // adds an item to cart
		if(!$price){
			$price = $this->Get_Item_Price($itemid,$qty);
		}
		if(!$info){
			$info = $this->Get_Item_Info($itemid);
		}

		if($this->itemqtys[$itemid] > 0)
		{ 	
			// the item is already in the cart..
			// so we'll just increase the quantity
			$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
			$this->Update_Cart_Total();
		} else {
			$this->items[]=$itemid;
			$this->itemqtys[$itemid] = $qty;
			$this->itemprices[$itemid] = $price;
			$this->iteminfo[$itemid] = $info;
		}
		$this->Update_Cart_Total();
	} // end of Add_Cart_Item


	function Edit_Cart_Item($itemid,$qty)
	{ // changes an items quantity

		if($qty < 1) {
			$this->Delete_Cart_Item($itemid);
		} else {
			$this->itemqtys[$itemid] = $qty;
			// uncomment this line if using 
			// the Get_Item_Price function
			// $this->itemprices[$itemid] = Get_Item_Price($itemid,$qty);
		}
		$this->Update_Cart_Total();
	} // end of Edit_Cart_Item


	function Delete_Cart_Item($itemid)
	{ // removes an item from cart
		$ti = array();
		$this->itemqtys[$itemid] = 0;
		foreach($this->items as $item)
		{
			if($item != $itemid)
			{
				$ti[] = $item;
			}
		}
		$this->items = $ti;
		$this->Update_Cart_Total();
	} //end of Delete_Cart_Item


	function Empty_Cart()
	{ // empties / resets the cart
		$this->total = 0;
		$this->itemcount = 0;
		$this->items = array();
		$this->itemprices = array();
		$this->itemqtys = array();
		$this->iteminfo = array();
	} // end of empty cart


	function Update_Cart_Total()
	{ // internal function to update the total in the cart
		$this->itemcount = 0;
		$this->total = 0;
		if(sizeof($this->items > 0))
		{
			foreach($this->items as $item) 
			{
				$this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
				$this->itemcount++;
			}
		}
	} // end of update_total


	function Get_Item_Price($item,$qty)
	{
		$sql="select productPrice from products where productCode='$item';";
		$query=mysql_query($sql);
		
		$row=mysql_fetch_assoc($query);
		$price=doubleval($row['productPrice']);
		return $price*$qty;
	}
	
	function Get_Item_Info($item)
	{
		$sql="select productName from products where productCode='$item';";
		$query=mysql_query($sql);
		
		$row=mysql_fetch_assoc($query);
		return $row['productName'];	
	}
}
?>
Return current item: shopcart