FILE: "catalog.php" __________________________________________________ ____________________
<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array ();
}
if (isset($_GET['buy'])) {
//Aggiunge l'oggetto alla fine dell'array $_SESSION['cart']
$_SESSION['cart'][] = $_GET['buy'];
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>product catalog</title>
</head>
<body>
Your shopping cart contains <?php
echo count($_SESSION['cart']); ?> items. </p>
view your cart</p>
<?php
$items= array(
'Dictionary',
'Parachute',
'Song of Beatles CD',
'Book');
$prices = array(24.95, 1000, 19.99, 34.95);
?>
<table border="1">
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < count($items); $i++) {
echo '<tr>';
echo '<td>' . $items[$i] . '</td>';
echo '<td>$' . number_format($prices[$i], 2) . '</td>';
echo '<td>Buy</td>';
echo '<tr>';
}
?>
</tbody>
</table>
</body>
</html>
__________________________________________________ _________________________
################################################## ##########
FILE: "cart.php"
__________________________________________________ _________________________
<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array ();
}
if (isset($_GET['empty'])) {
//Svuota l'array $_SESSION['cart']
unset($_SESSION['cart']);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Shopping Cart</title>
</head>
<body>
<h1>Your Shopping Cart</h1>
<?php
$items = array(
'Dictionary',
'Parachute',
'Song of Beatles CD',
'Book');
$prices = array(24.95, 1000, 19.99, 34.95);
?>
<table border="1">
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
for ($i = 0; $i < count($_SESSION['cart']); $i++) {
echo '<tr>';
echo '<td>' . $items[$_SESSION['cart'][$i]] . '</td>';
echo '<td align="right">$';
echo number_format($prices[$_SESSION['cart'][$i]], 2);
echo '</td>';
echo '</tr>';
$total = $total + $prices[$_SESSION['cart'][$i]];
}
?>
</tbody>
<tfoot>
<tr>
<th align="right">total</th>
<th align="right">$<?php echo number_format($total, 2); ?></th>
</tr>
</tfoot>
</table>
Continue Shopping or
<?php print(" <a href=\"cart.php?empty=1\">Empty your cart</a> "); ?>
</body>
</html>
__________________________________________________ _______________________
METTETELO SUL VOSTRO SERVER E PROVATELOE SOPRATUTTO COMMENTATE SE VI
E' STATO UTILE![]()
PS: NON CAMBIATE I NOME DEI FILE "catalog.php" e "cart.php"
(a meno che nn cambiate il codice :-P )