Simple shopping cart problem using session in PHP

tehsusenoh

New Member
i made simple PHP shoppingcart based on session. from bock "Build your own database driven web site using php & Mysql"its contain 3 files: the controller index.php, and the two template files catalog.html.php, cart.html.phpthe problem is when i click on "View your cart" link, the session will end and $_session['cart'] will unset automatically or somethingsorry i cant explain it very willso here is the codes:/* index.php - the controller*/ \[code\]$items = array( array('id' => '1', 'desc' => 'Candian-Australian Dictionary', 'price' => 24.95), array('id' => '2', 'desc' => 'As-new parachute (never opened)', 'price' => 1000), array('id' => '3', 'desc' => 'Songs of the Goldfish (2CD set)', 'price' => 19.99), array('id' => '4', 'desc' => 'Simply JavaScript (SitePoint)', 'price' => 39.95));session_start();if (!isset($_SESSION['cart'])){ $_SESSION['cart'] = array();}if (isset($_POST['action']) and $_POST['action'] == 'Buy'){ // Add item to the end of the $_SESSION['cart'] array $_SESSION['cart'][] = $_POST['id']; header('Location: .'); exit();}if (isset($_POST['action']) and $_POST['action'] == 'Empty cart'){ // Empty the $_SESSION['cart'] array unset($_SESSION['cart']); header('Location: ?cart'); exit();}if (isset($_GET['cart'])){ $cart = array(); $total = 0; foreach ($_SESSION['cart'] as $id) { foreach ($items as $product) { if ($product['id'] == $id) { $cart[] = $product; $total += $product['price']; break; } } } include 'cart.html.php'; exit();}include 'catalog.html.php';?>\[/code\]/* catalog.html.php - display all product*/\[code\]<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?><!DOCTYPE html PUBLIC "-//W3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Product catalog</title><meta http-equiv="content-type"content="text/html; charest=utf-8"/><style type="text/css">table { border-collapse: collapse;}td, th { border: 1px solid black;}</style></head><body><p>Your shopping cart contains <?php echo count($_SESSION['cart']); ?> items.</p><p><a href="http://stackoverflow.com/questions/2027823/?cart">View your cart</a></p><table border="1"> <thead> <tr> <th>Item Description</th> <th>Price</th> </tr> </thead> <tbody> <?php foreach ($items as $item): ?> <tr> <td><?php htmlout($item['desc']); ?></td> <td> $<?php echo number_format($item['price'], 2); ?> </td> <td> <form action="" method="post"> <div> <input type="hidden" name="id" value="http://stackoverflow.com/questions/2027823/<?php htmlout($item['id']); ?>"/> <input type="submit" name="action" value="http://stackoverflow.com/questions/2027823/Buy"/> </div> </form> </td> </tr> <?php endforeach; ?> </tbody></table><p>All prices are in imaginary dollars.</p></body></html>\[/code\]/* cart.html.php - display the products in the cart*/\[code\]<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?><!DOCTYPE html PUBLIC "-//W3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Shopping cart</title><meta http-equiv="content-type"content="text/html; charest=utf-8"/><style type="text/css">table { border-collapse: collapse;}td, th { border: 1px solid black;}</style></head><body><h1>Your Shopping Cart</h1><?php if (count($cart) > 0): ?><table> <thead> <tr> <th>Item Description</th> <th>Price</th> </tr> </thead> <tfoot> <tr> <td>Total:</td> <td>$<?php echo number_format($total, 2); ?></td> </tr> </tfoot> <tbody> <?php foreach ($cart as $item): ?> <tr> <td><?php htmlout($item['desc']); ?></td> <td> $<?php echo number_format($item['price'], 2); ?> </td> </tr> <?php endforeach; ?> </tbody></table><?php else: ?><p>Your cart is empty!</p><?php endif; ?><form action="?" method="post"> <p> <a href="http://stackoverflow.com/questions/2027823/?">Continue shopping</a> or <input type="submit" name="action" value="http://stackoverflow.com/questions/2027823/Empty cart"/> </p></form></body></html>\[/code\]thanks in advance
 
Back
Top