Hi,
Can someone please give me a hand to learn how to use this code in practical life. It's a very frustrating experience for me indead. I'm trying to solve this since who knows when. I tryed a lot of things, read all the forum stuff,etc... from starting session, to adding stuff,... & nothing worked till now. I'm desperate & also willing to understand how that works indaed.
Yes it's my first experience with carts & those -> thingsso,... I hope it's normal.
The code is saying:
To add items to a basket use
$basket->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data);
but,... I couldn't make it work.
Yes, here's the complete code so that everybody can benefit from this experience (till now I succeded to benefit only headaches, I'm really desperate you know. :-( Please give me a hand befor I'll commit a suicide. I really like php,...
& yes, also the code looks very cool, & quite nice to learn from, but... I'm not capable to make it work or to understand how to add or delate things. The code:
<?php
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
(at your option) any later version.
#
class Basket {
var $basket_count;
var $basket_item_id;
var $basket_item_name;
var $basket_item_quantity;
var $basket_item_data;
var $basket_item_price;
function Basket() {
$this->basket_count=0;
}
function Add_Item($ID,$name,$quantity=1,$price=0,$data='') {
$this->basket_item_id[$this->basket_count]=$ID;
$this->basket_item_name[$this->basket_count]=$name;
$this->basket_item_quantity[$this->basket_count]=$quantity;
$this->basket_item_data[$this->basket_count]=$data;
$this->basket_item_price[$this->basket_count]=$price;
$this->basket_count++;
return ($this->basket_count-1);
}
function Del_Item($pos) {
$this->basket_item_id[$pos]='';
}
function Get_Item_ID($pos) {
return $this->basket_item_id[$pos];
}
function Get_Item_Name($pos) {
return $this->basket_item_name[$pos];
}
function Get_Item_Price($pos) {
return $this->basket_item_price[$pos];
}
function Get_Item_Quantity($pos) {
return $this->basket_item_quantity[$pos];
}
function Get_Item_Data($pos) {
return $this->basket_item_data[$pos];
}
function Set_Item_Quantity($pos,$quantity) {
$this->basket_item_quantity[$pos]=$quantity;
}
function Set_Item_Data($pos,$data) {
$this->basket_item_data[$pos]=$data;
}
function Enum_Items($start=false) {
static $current;
if ($current>=$this->basket_count) return -1;
if (!$start) {
$current++;
} else {
$current=0;
}
while (($this->basket_item_id[$current]=='') && ($current<$this->basket_count)) {
$current++;
}
return ($current<$this->basket_count) ? $current : -1;
}
function Empty_Basket() {
$this->basket_count=0;
}
function Get_Basket_Count() {
$num=0;
for ($i=0;$i<$this->basket_count;$i++) {
if ($this->basket_item_id[$i]!='') $num++;
}
return $num;
}
}
?>
& yes I also tryed to add that session, but... Here the complete code explanation. It's a real frustration to make it understand. At least for me:
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)
session_name("mysession");
session_start();
if (! ssession_is_registered("basket") ) {
$basket=new Basket;
session_register("basket");
}
To add items to a basket use
$basket->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data);
the item id is the unique ID for the item added to the basket. (ie for lookup in a databasse)
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. so you can't just start as pos 0 and goto 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();
}
}
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
And if you use this on a site let me know. I'd like to see it in action elsewere.
Can someone please give me a hand. I just wanna learn those => -> stuff that I just don't understand. Please be patient.
Thanks.
Mauro