mindapolis Posted August 17, 2011 Report Posted August 17, 2011 I hesitated posting this because most of my errors are stupid mistakes on my end. however, I'm doing the shopping cart video series and I'm stuck at the very end of video 8. I have watched it three or four times and for some reason the quantity isn't matching up with the price. here's the code: shoppingcart.php <?php class shoppingCart { protected $items = array(); public function addItem($product_id) { if (array_key_exists($product_id , $this->items)) $this->items[$product_id] = ($this->items[$product_id] +1); else $this->items[$product_id] = 1; } public function GetItemCost($product_id) { $cost_string = get_item_cost($product_id); $cost_float = "$cost_string" + 0; return $cost_float * $this->GetItemQuantity[$product_id]; } public function getItems() { return array_keys($this->items); } public function GetItemQuantity($product_id) { return intval($this->items[$product_id]); } public function emptyCart() { $this->items = array(); } } functions.php <?php require_once('classes/shoppingCart.php'); /**DEFINE GLOBALS**/ define('STORE_XML_FILE' , 'catalog.xml'); /*FUNCTIONS*/ session_start(); function render_products_from_xml() { $output = '.<table class="product"> <tr>'; foreach(get_xml_catalog() as $product) { $output .=' <td class="product"> <h2>'.$product->title.'</h2> <div> <img src="'.$product->img. '" /> <span> '.$product->description.' </span> </div> <div class="price"> '.$product->price.' </div> <div class="addToCart"> <a href="checkOut.php?id='.$product->id.'">add to cart</a> </div> </td>'; } $output .=' </tr> </table> '; echo $output; } 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['cart']); } function set_shopping_cart($cart) { $_SESSION['cart'] = serialize($cart); } function product_exist($product_id) { foreach(get_xml_catalog() as $product) { if ($product->id == $product_id) return true; } return false; } function get_item_cost($product_id) { foreach(get_xml_catalog() as $product) { if ($product_id == $product->id) return $product->price; } throw new Exception('item not found' . $product_id); } ?> 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(); if (isset($_GET["id"])) { $product_id = htmlspecialchars($_GET["id"]); } if (product_exist($product_id)) { $shopping_cart->addItem($product_id); } ?> <table class="shoppingCart"> <tr> <th> Product ID </th> <th> quantity </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(ShoppingCart $shopping_cart , $product_id , $line_item_counter) { $quantity = $shopping_cart->GetItemQuantity($product_id); $amount = $shopping_cart->GetItemCost($product_id); $unit_cost = get_item_cost($product_id); $output = " <tr> <td> $product_id </td> <td> $quantity </td> <td> $$amount </td> </tr>"; return $output; } ?> </table> <?php set_shopping_cart($shopping_cart); ?> </body> </html>
mindapolis Posted August 20, 2011 Author Report Posted August 20, 2011 Has anyone had this problem before? ? ? ? ?
falkencreative Posted August 20, 2011 Report Posted August 20, 2011 I'm not immediately seeing anything that should be causing you problems, but obviously I must be missing something. Are the prices calculated in a logical way? Is there a pattern in what prices display, and the quantity of items added? Also, you've checked your XML file, and ensured that there are no spaces before/after the price, and that there are no "$" characters in price within the XML file?
mindapolis Posted August 20, 2011 Author Report Posted August 20, 2011 can I email you tomorrow with the answers to your questions
falkencreative Posted August 20, 2011 Report Posted August 20, 2011 can I email you tomorrow with the answers to your questions Yeah, that's fine.
Recommended Posts