Hello!!!
Thank you very much for answering my post.
The shoppingcart is used with 3 files. Index.php as a controler. the other two files are used as output html. Here is the code. The session is as array.
I am sorry for the long code but here I let you the all code:
index.php code:
<?php
include_once $_SERVER['DOCUMENT_ROOT'] .
'/sitepoint/shoppingcart/includes/magicquotes.inc.php';
$items = array(
array('id' => '1', 'desc' => 'Canadian-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';
?>
catalog.html.php code:
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/sitepoint/shoppingcart/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; charset=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="?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="<?php
htmlout($item['id']); ?>"/>
<input type="submit" name="action" value="Buy"/>
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p>All prices are in imaginary dollars.</p>
</body>
</html>
cart.html.php Code:
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/sitepoint/shoppingcart/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; charset=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="?">Continue shopping</a> or
<input type="submit" name="action" value="Empty cart"/>
</p>
</form>
</body>
</html>
magicquotes.inc.php Code:
<?php
if (get_magic_quotes_gpc())
{
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
?>
?>
helpers.inc.php Code:
<?php
function html($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
function htmlout($text)
{
echo html($text);
}
function bbcode2html($text)
{
$text = html($text);
// [B]old
$text = preg_replace('/\\[B](.+?)\\[\\/B]/i',
'<strong>$1</strong>', $text);
// [I]talic
$text = preg_replace('/\\[I](.+?)\\[\\/I]/i', '<em>$1</em>', $text);
// Convert Windows (\\r\
) to Unix (\
)
$text = str_replace("\\r\
", "\
", $text);
// Convert Macintosh (\\r) to Unix (\
)
$text = str_replace("\\r", "\
", $text);
// Paragraphs
$text = '<p>' . str_replace("\
\
", '</p><p>', $text) . '</p>';
// Line breaks
$text = str_replace("\
", '<br/>', $text);
// link
$text = preg_replace(
'/\\([-a-z0-9._~:\\/?#@!$&\\'()*+,;=%]+)\\[\\/URL]/i',
'<a href="$1">$1</a>', $text);
// [link](url)
$text = preg_replace(
'/\\[URL=([-a-z0-9._~:\\/?#@!$&\\'()*+,;=%]+)](.+?)\\[\\/URL]/i',
'<a href="$1">$2</a>', $text);
return $text;
}
function bbcodeout($text)
{
echo bbcode2html($text);
}
?>
Thank you very much!!!