Hi there,
Sorry for all the questions. I'm a newbie trying to work my way through the Killer PHP video tutorials and everything was fine until I hit the shopping cart tutorial.
The approach that the video tutorial takes is to create a ShoppingCart class and then create a shoppingcart object. I am running into several problems.
Step #1- Checking to see if the Session Variable is set. If it is not, then a constructor is used to instantiate an object of ShoppingCart class. If it is set, then it is to return the variable in SESSION. (This value is later serialized.)
function get_shopping_cart() {
if(!isset($_SESSION["cart"])) {
return new ShoppingCart;
} else {
return unserialize($_SESSION["cart"]);
}
}
$shopping_cart = get_shopping_cart();
Step #2: Use the methods defined in ShoppingCart class on the shopping_cart object. For example:
$shopping_cart->AddItem($product_id);
PROBLEM: This works just fine when SESSION['cart'] has not been set and therefore an object is created. However, I run into a problem when it is already set. I receive the error "call to a member function of a nonobject"
Step #3: If Steps 1 and 2 work ok, then my next question is about the code in the tutorial for AddItem method in the ShoppingCart class.
public function AddItem($product_id) {
if(array_key_exists($product_id, $this->items)) {
$this->items[$product_id] = $this->items[$product_id] + 1;
} else {
$this->items[$product_id] = 1;
}
PROBLEM: For some reason, after an item is added the first time around, if I hit the back button and try to add it again, the first one that I added disappears. Is it something wrong with the code or sessions? I have session_start() at the beginning of every page. I'm stumped! :/