PHP Classes

basketlib: Shopping basket class

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2003-11-09 (12 years ago) RSS 2.0 feedStarStarStar 53%Total: 7,610 All time: 226 This week: 1,043Down
Version License PHP version Categories
basketlib 1.0GNU Lesser Genera...3E-Commerce
Description Author

This class can be used to manage the contents of a shopping basket.

It can add items, remove items, get or set the properties of each item like the identifier, quantity, price, name and custom data.

The class can also calculate the total of price of all items in the basket, search for items in the basket and get the next available position in the items list.

Picture of Edward Rudd
Name: Edward Rudd <contact>
Classes: 2 packages by
Country: United States United States
Age: 37
All time rank: 969 in United States United States
Week rank: 576 Down58 in United States United States Down

I wrote this for a website I designed and, well, I might as well share it with 
the rest of the world. It is finished and working in it's current for, but
there are some extra functions I wish to add at a later date.

To actually use the basket put ssomething like this in the beginning of your
pages of the site or in a prepended included file. (auto_prepend)
### php4 < 4.1.0
if (! session_is_registered("basket") ) {
	$basket=new Basket;
### php4 >= 4.1.0
if (! $_SESSION['basket']) {
	$_SESSION['basket']=new Basket;

To add items to a basket use
### php4 < 4.1.0
### php4 >= 4.1.0

item id:  is the unique ID for the item added to the basket. (ie for lookup 
	in a databasse)
display name: can be used for a description for the item (possibly for pulling
	into a view basket).
quantity: is self explanitory and defaults to one.
price: is the price of the item. (this is for future functionality) 
	defaults to 0.
data: is for any extra data to be associated with the item (ie. for a card the
	name and message) just store an associated array ($data["firstname"]="Jon")

the Del_* Get_* Set_* items get and set the different fields and require a $pos identifier returned from Enum_Items (position in the basket)

when Enum_Items  is first called pass a true parameter to start the enumaration from the beginning of the basket.
ever call after that pass either nothing of false to get the next item.
It returns -1 when the end of the basket is reached.

This procedure skips over deleted items.  You can't just start as pos 0	and 
	go to Get_Basket_Count

Here is sample code to enumerate the basket.

if ($basket->Get_Basket_Count()>0) {  # are there items in the basket
	$pos = $basket->Enum_Items(true);
	while ($pos>=0) {
		print $basket->Get_Item_Name($pos)."-".$basket->Get_Item_Quantity($pos)."<BR>";
		$pos = $basket->Enum_Items();
for php4>=4.1.0 replace $basket with $_SESSION['basket']

The function Search_Item will search for the first item in the basket of the
ID, You can use this to find an exisiting item w/o having to use Enum_Item to,
for example, increment and existing item's quantity instead of adding in a new
item of the same ID.
$pos = $basket->Search_Item($ITEM_ID);
if ($pos==-1) {
} else {
for php4>=4.1.0 replace $basket with $_SESSION['basket']

Inc_Item_Quantity will increment the quantity of the given item by one.

Get_Basket_Count returns the number of undeleted items in the basket. 
(deleted items just get flagged so the data is still in the basket).

Well there it is.
If you have any comments please feel free to send them my way to, or even better, goto and post 
bugs, feature requests, etc, on the project there..  The lastest version will
also be available there.

And if you use this on a site let me know.  I'd like to see it in action
  Files folder image Files  
File Role Description
Plain text file basketlib.php Class The basketlib class file.
Accessible without login Plain text file COPYING Lic. GNU LGPL
Accessible without login Plain text file README Doc. About this class and instructions.

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:226
This week:1,043Down
 User Ratings  
 All time