mindapolis Posted August 6, 2011 Report Share Posted August 6, 2011 Why am I getting this error? Fatal error: Class 'ShoppingCart' not found in D:\Hosting\5246561\html\functions.php on line 17 functions.php <?php require_once('classes/shoppingCart.php'); /**DEFINE GLOBALS**/ define('STORE_XML_FILE' , 'catalog.xml'); /*FUNCTIONS*/ function get_xml_catalog() { return new SimpleXMLElement(file_get_contents(STORE_XML_FILE)); } function get_shopping_cart() { if (! isset($_SESSION['cart'])) return new ShoppingCart(); //starts new cart else return unserialize($_SESSION['Get']); } function set_shopping_cart($cart) { $_SESSION['cart'] = unserialize($cart); } function product_exist($product_id) { foreach(get_xml_catalog() as $product) { if ($product->id = $product_id) return true; } return false; } ?> shoppingCart.php in the class folder which is uploaded <?php class shoppingCart { protected $items = array(); public function addItem($product_id) { if (array_key_exists($product_id . $this->item)) $this->item[$product_id] = $this->item[$product_id] +1; else $this->item[$product_id] = 1; } public function getItems() { return array_keys($this->item); } } ?> Link to comment Share on other sites More sharing options...
falkencreative Posted August 6, 2011 Report Share Posted August 6, 2011 Fatal error: Class 'ShoppingCart' not found in D:\Hosting\5246561\html\functions.php on line 17 I bet it's a capitalization issue. "ShoppingCart" is no the same as "shoppingCart". (Note your class has the first letter lowercase.) Link to comment Share on other sites More sharing options...
mindapolis Posted August 6, 2011 Author Report Share Posted August 6, 2011 didn't help the error is on checkOut.php functions.php <?php require_once('classes/shoppingCart.php'); /**DEFINE GLOBALS**/ define('STORE_XML_FILE' , 'catalog.xml'); /*FUNCTIONS*/ session_start(); function get_xml_catalog() { return new SimpleXMLElement(file_get_contents(STORE_XML_FILE)); } function get_shopping_cart() { if (! isset($_SESSION['cart'])) return new shoppingCart(); //starts new cart else return unserialize($_SESSION['Get']); } function set_shopping_cart($cart) { $_SESSION['cart'] = unserialize($cart); } function product_exist($product_id) { foreach(get_xml_catalog() as $product) { if ($product->id = $product_id) return true; } return false; } ?> checkout.php <?php require_once('functions.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $shopping_cart = get_shopping_cart(); $product_id=$_REQUEST['id']; if (product_exists($product_id)) { $shopping_cart->addItem($product_id); } ?> <table class="shoppingCart"> <tr> <th> Produt ID </th> <th> Quanity </th> <th> Amount </th> </tr> <?php $line_item_counter = 1; foreach($shopping_cart->GetItems() as $product_id) { echo render_shopping_cart_row($shopping_cart, $product_id , $line_item_counter); $line_item_counter++; } function render_shopping_cart_row(shopping_cart $shopping_cart, $product_id , $line_item_counter) { $quanity = $shopping_cart->GetItemQuuanity(#product_id); $output = " <tr> <td> $product_id </td> <td> $quanity </td> <td> amount </td> </tr> '; return $output; } ?> </table> <?php set_shopping_cart($shopping_cart); ?> </body> </html> Link to comment Share on other sites More sharing options...
falkencreative Posted August 6, 2011 Report Share Posted August 6, 2011 I basically guarantee the error comes from the capitalization issue. "ShoppingCart" is not the same thing as "shoppingCart". Even if the error message says it is from checkOut.php, you'll notice that it is calling $shopping_cart = get_shopping_cart(); and the get_shopping_cart() function calls this line within functions.php: return new shoppingCart(); //starts new cart That either needs to be "ShoppingCart()", or you need to change your shopping cart class to "shoppingCart". Link to comment Share on other sites More sharing options...
mindapolis Posted August 6, 2011 Author Report Share Posted August 6, 2011 That didn't help. Here's the revised code. <?php class shoppingCart { protected $items = array(); public function addItem($product_id) { if (array_key_exists($product_id , $this->item)) $this->item[$product_id] = $this->item[$product_id] +1; else $this->item[$product_id] = 1; } public function getItems() { return array_keys($this->item); } public function GetItemQuuanity($product_id) { return $this->item[$product_id]; } } ?> Link to comment Share on other sites More sharing options...
falkencreative Posted August 6, 2011 Report Share Posted August 6, 2011 What's the exact error you are getting? Copy/paste the full details please? Also -- probably not related to your error -- note the misspelling here: public function GetItemQuuanity($product_id) Link to comment Share on other sites More sharing options...
mindapolis Posted August 6, 2011 Author Report Share Posted August 6, 2011 Fatal error: Class 'shoppingCart' not found in D:\Hosting\5246561\html\functions.php on line 17 Link to comment Share on other sites More sharing options...
falkencreative Posted August 6, 2011 Report Share Posted August 6, 2011 Is this line within functions.php correct? require_once('classes/shoppingCart.php'); Is the path correct, and is it capitalized/not capitalized correctly? "ShoppingCart.php" is not the same as "shoppingCart.php". Sounds like it may be unable to see your class file? Link to comment Share on other sites More sharing options...
mindapolis Posted August 6, 2011 Author Report Share Posted August 6, 2011 I found the error. Somehow there were two shoppingCart.php files and the correct one wasn't in the classes folder. Now,, there is another error but let me see if I can figure it out. thanks so much for helping! Link to comment Share on other sites More sharing options...
mindapolis Posted August 6, 2011 Author Report Share Posted August 6, 2011 Okay, I figured out one error, but am stuck on another one. Warning: unserialize() expects parameter 1 to be string, object given in D:\Hosting\5246561\html\functions.php on line 24 <?php require_once('classes/shoppingCart.php'); /**DEFINE GLOBALS**/ define('STORE_XML_FILE' , 'catalog.xml'); /*FUNCTIONS*/ session_start(); function get_xml_catalog() { return new SimpleXMLElement(file_get_contents(STORE_XML_FILE)); } function get_shopping_cart() { if (! isset($_SESSION['cart'])) return new shoppingCart(); //starts new cart else return unserialize($_SESSION['Get']); } function set_shopping_cart($cart) { $_SESSION['cart'] = unserialize($cart); } function product_exist($product_id) { foreach(get_xml_catalog() as $product) { if ($product->id = $product_id) return true; } return false; } ?> Link to comment Share on other sites More sharing options...
falkencreative Posted August 6, 2011 Report Share Posted August 6, 2011 Isn't this section of code: function set_shopping_cart($cart) { $_SESSION['cart'] = unserialize($cart); } supposed to be using "seriealize" not "unserialize"? Link to comment Share on other sites More sharing options...
Recommended Posts