Topic: Shopping cart tutorial quantity won't go up start session problem
Hi everyone,
I'm am following Jon Lebensold's shopping cart tutorial, I am using Dreameaver CS3 instead of Eclipse. I'm am at the end of video 7, I've finished the $quantity code and started the session in my functions file.
However when I try to refresh my page in the browser the quantity does not go up at all.
I've looked at my php error logs for apache, they state:
[06-Oct-2009 11:30:16] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/mywebsite/templates.php:69) in /Applications/MAMP/htdocs/mywebsite/functions.php on line 11
(So I am aware that this is the problem but I have no idea how to sort it out, could somebody please help me. I have attached the code for the templates file, the functions file and the shopping cart class. I'll say thanks in advance.)
My code for php templates, looks like this:
<?php
function render_header($title = 'My Shop')
{
return '
<!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>
<title>'.$title.'</title>
<link href="css/shop.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="nav">
<a href="#">view store</a>
<a href="#">view cart</a>
<a href="#">clear cart</a>
</div>
';
}
function render_footer()
{
return '
</div>
</body>
</html>';
}
function render_products_from_xml()
{
$output = '<table class="products">
<tr>';
foreach(get_xml_catalog() as $product)
{
$output .='
<td class="product">
<h2>'.$product->title.'</h2>
<div>
<img src="'.$product->img.'" height="151" width="100" />
<span>
'.$product->description.'
</span>
</div>
<div class="size">
'.$product->size.'
</div>
<div class="price">
'.$product->price.'
</div>
<div class="addToCart">
<a href="addToCart.php?id='.$product->id.'">add to cart</a>
</div>
</td>';
}
$output .='
</tr>
</table>
';
return $output;
}
?>My code for functions looks like this:
<?php
/** DEFINE GLOBALS **/
define('STORE_XML_FILE','catalog.xml');
/** DEFINE REFERENCES **/
require_once('templates.php');
require_once('ShoppingCart.php');
/** 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();
else
return unserialize($_SESSION['cart']);
}
function set_shopping_cart($cart)
{
$_SESSION['cart'] = serialize($cart);
}
function product_exists($product_id)
{
foreach(get_xml_catalog() as $product)
{
if ($product->id == $product_id)
return true;
}
return false;
}
?>My code for the ShoppingCart class looks like this:
<?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 GetItems()
{
return array_keys($this->items);
}
public function GetItemQuantity($product_id)
{
return $this->items[$product_id];
}
}
?>Last edited by slam38 (October 6, 2009 6:14 am)

