PHP Classes

basketlib: Shopping basket class

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 52%Total: 7,668 All time: 235 This week: 64Up
Version License PHP version Categories
basketlib 1.0.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: 45
All time rank: 9911 in United States United States
Week rank: 178 Down22 in United States United States Down

Details

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.

  Files folder image Files (3)  
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.

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:7,668
This week:0
All time:235
This week:64Up
 User Ratings  
 
 All time
Utility:76%StarStarStarStar
Consistency:77%StarStarStarStar
Documentation:67%StarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:52%StarStarStar
Rank:2421