Codice PHP:
<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
if (isset($_GET['buy'])) {
// Add item to the end of the $_SESSION['cart'] array
$_SESSION['cart'][] = $_GET['buy'];
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>
<!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">
<head>
<title>Product catalog</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
Your shopping cart contains <?php echo count($_SESSION['cart']); ?> items.</p>
[url="cart.php"]View your cart[/url]</p>
<?php
$items = array(
'Canadian-Australian Dictionary',
'As-new parachute (never opened)',
'Songs of the Goldfish (2CD Set)',
'Ending PHP4 (O\'Wroxey Press)');
$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><a href="' . $_SERVER['PHP_SELF'] .
'?buy=' . $i . '">Buy</a></td>';
echo '</tr>';
}
?>
</tbody>
</table>
All prices are in imaginary dollars.</p>
</body>
</html>
al posto dell'array come mostro il contenuto di due campi di database?