Jump to content

error meaning


mindapolis

Recommended Posts

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

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

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

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

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

×
×
  • Create New...