Jump to content

[SOLVED] function not foud -- but it is sooo there


dumdumsareyum

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.