christcb Posted April 6, 2010 Share Posted April 6, 2010 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 Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/ Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 $cart isn't an object. Where is it being defined? Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037631 Share on other sites More sharing options...
christcb Posted April 6, 2010 Author Share Posted April 6, 2010 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 Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037632 Share on other sites More sharing options...
anupamsaha Posted April 6, 2010 Share Posted April 6, 2010 Where did you include the class definition file? Thanks Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037636 Share on other sites More sharing options...
christcb Posted April 6, 2010 Author Share Posted April 6, 2010 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 Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037638 Share on other sites More sharing options...
anupamsaha Posted April 6, 2010 Share Posted April 6, 2010 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. Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037643 Share on other sites More sharing options...
christcb Posted April 6, 2010 Author Share Posted April 6, 2010 DUH *smack forehead* i used to know that, thank you so much for your help that works great. Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037647 Share on other sites More sharing options...
anupamsaha Posted April 6, 2010 Share Posted April 6, 2010 You are welcome. Please mark this thread as "solved". Thanks. Link to comment https://forums.phpfreaks.com/topic/197722-fatal-error-call-to-a-member-function-add-on-a-non-object/#findComment-1037649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.