#native_company# #native_desc#
#native_cta#

Simple Cart

By Wouter v G
on March 31, 2003

Version: 0.1

Type: Class

Category: Shopping Carts

License: GNU General Public License

Description: Just a simple cart, does work with mysql. It’s just a start, it may help someone 🙂

<?	
	/*
		# (Current) Functions List
			- addItem		( itemID, count )	= add an item
			- removeItem 	( itemID )			= remove an item
			- getPrice		( )					= get's the total price of the items
			- saveItems		( )					= save the items to a session
			- clearItems	( )					= clear the session and the array with items
		#
		
		# Disclaimer :P
			- Copyright (C) 
			  This program is free software; you can redistribute it and/or modify it under the terms 
			  of the GNU General Public License as published by the Free Software Foundation; either version 2 
			  of the License, or any later version. 
			  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
			  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
			  See the GNU General Public License for more details. 
			  http://www.opensource.org/licenses/gpl-license.html 
			- Questions? [email protected]
		#
		
		# MySQL Table
			CREATE TABLE products ( 
				ID int(4) unsigned NOT NULL auto_increment, 
				name varchar(255) default '0', 
				price varchar(8) default '0', 
				PRIMARY KEY (ID) 
			) TYPE=MyISAM; 
		#		
	*/

	error_reporting(E_ALL);

	class Cart {
	
		var $buy;
		var $count;
	
		function Cart ()
		{
		
			// Set $this->buy to be an array
			$this->buy = array();
		
		}
		
		function __error( $ERR )
		{
		
			// Dirty just make your own
			die ( $ERR );
		
		}
		
		function addItem ( $ID, $COUNT )
		{
		
			// If the ID wasn't set or wasn't numeric
			// Or if the COUNT wasn't set or wasn't numeric
			if( Empty ( $ID ) OR !is_numeric ( $ID ) OR Empty ( $COUNT ) OR !is_numeric ( $COUNT ) )
				$this->__error ( "There wasn't an ID or the ID wasn't numeric." );
			
			// else
			for( $i = 0; $i < $COUNT; $i++ )
				$this->buy[$ID] = $COUNT;
			
			// count all the items
			$this->count = count($this->buy);	
		
		}
		
		function removeItem ( $ID )
		{
	
			// If the ID wasn't set or wasn't numeric
			if( Empty ( $ID ) OR !is_numeric ( $ID ))
				$this->__error ( "There wasn't an ID or the ID wasn't numeric." );

			// else
			unset( $this->buy[$ID] );
	
		}
		
		function getPrice()  
		{ 
		
			$price = 0; 
			
			foreach ( $this->buy AS $ID => $COUNT ) 
			{ 
			
				// Select the price of the product
				$query = mysql_fetch_array(mysql_query("SELECT price FROM products WHERE ID = '" . $ID . "' "));

				// Get the price of the product and put it to an variable
				$price = $price + ($COUNT * $query["price"]); 
				
			}
			
			return $price; 

	    }
		
		function saveItems ()
		{	
			
			// if there are items,
			// serialize the data en save it in a session
			if ( IsSet ( $this->count ) )
				$_SESSION['items'] = serialize($this->buy);
							
		}

		function clearItems ()
		{
		
			// Delete the array "buy"
			unset($this->buy);
			// unset the session
			unset($_SESSION['items']);
		
		}
	
	}
	
?>