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
session_name("mysession");
session_start();
if (! session_is_registered("basket") ) {
$basket=new Basket;
session_register("basket");
}
### php4 >= 4.1.0
session_name("mysession");
session_start();
if (! $_SESSION['basket']) {
$_SESSION['basket']=new Basket;
}
To add items to a basket use
### php4 < 4.1.0
$basket->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data);
### php4 >= 4.1.0
$_SESSION['basket']->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data);
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.
*NOTE*
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();
}
}
*NOTE*
for php4>=4.1.0 replace $basket with $_SESSION['basket']
*NEW*
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.
example:
$pos = $basket->Search_Item($ITEM_ID);
if ($pos==-1) {
$basket->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data);
} else {
$basket->Inc_Item_Quantity($pos);
}
*NOTE*
for php4>=4.1.0 replace $basket with $_SESSION['basket']
*NEW*
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
eddie@omegaware.com, or even better, goto http://urklesphp.sf.net/ 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
elsewere.
|