dumdumsareyum Posted May 14, 2008 Share Posted May 14, 2008 I get this error Fatal error: Call to undefined function delete_item() in C:\wamp\www\PlanHouse\inc\ShoppingCart.php on line 106 when trying to update items in my shopping cart......i copied and pasted the name of the function for the function call to avoid typos but it still says it's undefined. Here's the relevant bit (I think): //Method for updating an item in the cart //Takes 3 arguments, the item id, the option code and the quantity public function update_item($id, $optCode, $qty) { if(($qty > 0) && ($qty != $this->items[$id][$optCode]['qty'])) { $this->items[$id][$optCode]['qty'] = $qty; //Print a message echo "<p>You now have $qty copy(ies) of the plan $id option code $optCode in your cart.</p>\n"; } if($qty == 0) {delete_item($id, $optCode); //this is the line giving the error } } //end of update_item //Method for deleting an item in the cart public function delete_item($id, $optCode) { //Confirm that it's in the cart if (isset($this->items[$id][$optCode])) { //Print message: echo "<p>The plan #$id with option code $optCode has been removed from your shopping cart.</p>\n"; //Remove item unset($this->items[$id][$optCode]); } } //end of delete_item method. What am I missing? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 That's in a class....you need to instantiate an object of that class. Quote Link to comment Share on other sites More sharing options...
severndigital Posted May 14, 2008 Share Posted May 14, 2008 if($qty == 0) {delete_item($id, $optCode); //this is the line giving the error } } //end of update_item //Method for deleting an item in the cart public function delete_item($id, $optCode) { looks like you are putting the cart before the horse .. define the function before calling it. Quote Link to comment Share on other sites More sharing options...
dumdumsareyum Posted May 14, 2008 Author Share Posted May 14, 2008 here is the code where I call the update cart method: foreach($qty as $id => $info) { echo "id is $id"; foreach($info as $optCode => $value) { $cart->update_item($id, $optCode, $value); } } the value of the $cart object is retrieved from a session variable -- the, I know it is getting the cart object ok because it is displaying the cart, and when the call to update the cart is made for a non-zero amount and the if statement with the call to the delete item is removed, the cart updates and displays properly. But even if a nonzero term is entered and the if statement for if($qty == 0) is in the code, it dies. Also, I tried switching the order of the functions already so delete_item was first, it just get the same error but occurring on the line where the function call is moved to. Quote Link to comment Share on other sites More sharing options...
dumdumsareyum Posted May 14, 2008 Author Share Posted May 14, 2008 yay! I was sitting there staring at it and somewhere in the back of my brain called out "remember what you read about scope resolution operators!" I changed the function call from delete_item($id, $optCode) to ShoppingCart::delete_item($id, $optCode) and voila suddenly knew what the heck it was. Classes are weird. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 You could use $this->delete_item. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.