Jump to content

Fatal error: Call to a member function add() on a non-object


christcb

Recommended Posts

I am trying to make a simple shopping cart.  I created the add($item) function to add an item to the cart.  When I add the first item it works fine, but when I try to add a second item (or another of the first item) I get the error "Fatal error: Call to a member function add() on a non-object ".

 

The code in the file where I get the area is :

    if (isset($_POST["quantity"])) {
        $quantity = $_POST["quantity"];
        $item = new item("Nature of New Testament Church", "NoNTCeflyer310", $quantity, 11.95);
        $cart->add($item);
    } //end if isset quantity

 

the error indicates the $cart->add($item); line

Cart is saved in _SESSION and assigned a couple lines above here is the code :

 

    if (isset($_SESSION['cart'])) {
        $cart = $_SESSION['cart'];
    } else {
        $cart = new cart();
    } //if to set cart variable in session or create new cart

It's in another file require("scb_common.php")

    class cart {
        public $head = false;  //first item in cart
        public $tail = false;  //last item in cart
        public $discount;  //apply discount for website specials
        public $items = 0;
        public $total = 0;

        //add item to cart
        public function add($item) {
            //if there are items in list
            if($this->head) {
                $current = $this->head;
                while ($current) {  //search through list
                    //if item already in list add quantity
                    if($item->itemNum == $current->itemNum) {
                        $current->quantity += $item->quantity;
                        echo "adding ".$item->quantity;
                        break;
                    } else {
                        $current = $current->next;
                    } //end if item found
                } // end while searching
                //if not found add to end of list
                $this->tail->next=$item;
                $item->prev=$this->tail;
                $this->tail=$item;
                $this->items++;
            } else { //else create new list
                $this->head=$item;
                $this->tail=$item;
                $this->items++;
            } //end if to add to list
            $this->setdiscount();
        } //add

 

Ok, I guess, its an issue with storing object in a session. Please try the following:

 

The section where you are storing the cart object to a session, do this:

 

$_SESSION['cart'] = serialize($cart);

 

And, the section from where you read the cart object from the session, do this:

 

$cart = unserialize($_SESSION['cart']);

 

Hope, this will help.

 

Thanks.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.