Ciao a tutti
ho realizzato un semplice carrello della spesa:
alla pagina confirm.php mi arrivano codice e prezzo che ottengo con una request e che mantengo con le sessioni in questo modo:
Codice PHP:
$_SESSION['codice'] = $_REQUEST['codice'];
$codice = $_SESSION['codice'];
(e faccio la stessa cosa per il prezzo)
chiedo se l' utente vuole confermare l' acquisto dell artico $codice al costo di $prezzo!
cliccando su "compra" passo al cart.php la $i (che mi serve per incrementare l'array cart)
al cart.php arrivano dunque la $i, e con la session start riprendo il lcodice e prezzo e con un ciclo inserisco tutto nel carrello.
IL PROBLEMA è:
se aggiungo al carrello un articolo con codice: 010203 e prezzo 19.90
e poi aggiungo un altro articolo con codice: 000123 e prezzo 39.90
anziche ottenere:
CODICE | PREZZO
------------------
010203 - 19.90
000123 - 39.90
ottengo:
CODICE | PREZZO
------------------
000123 - 39.90
000123 - 39.90
si ricorda praticamente solo dell' ultima cosa che aggiungo, credo che sto sbagliando l ' utilizzo delle sessioni o forse sbaglio ad usare l array?
qualcuno può aiutarmi a risolvere questo problema?
vi posto i codici delle pagine! grazie in anticipo 
PAGINA DI CONFERMA CHIAMATA "catalog.php" DOVE ARRIVANO I DATI CODICE E PREZZO:
Codice PHP:
<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array ();
}
?>
<!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>conferma</title>
</head>
<body>
<h1>Conferma acquisto</h1>
il tuo carrello contiene <?php
echo count($_SESSION['cart']); ?> articoli. </p>
<?php
$_SESSION['codice'] = $_REQUEST['codice'];
$_SESSION['prezzo'] = $_REQUEST['prezzo'];
$codice = $_SESSION['codice'];
$prezzo = $_SESSION['prezzo'];
$items = array($codice);
$prices = array($prezzo);
?>
<table border="0">
<thead>
<tr>
<th>Codice</th>
<th>Prezzo</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> | Vuoi davvero aggiungere questo articolo al carrello?_<a href=\"cart.php?codice=$codice&prezzo=$prezzo&buy=$i\">Compra!</a></td>";
echo '<tr>';
}
?>
</tbody>
</table>
[url="dati_articolo.htm"]Shopping[/url] | [url="cart.php"] il tuo carrello[/url]</p>
</body>
</html>
__________________________________________________ ________________________
il carrello cart.php
Codice 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();
}
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>Carrello</h1>
<?php
$prezzo = $_SESSION['prezzo'];
$codice = $_SESSION['codice'];
$items = array($codice);
$prices = array($prezzo);
?>
<table border="1">
<thead>
<tr>
<th>Codice</th>
<th>Prezzo</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>
[url="dati_articolo.htm"]Shopping[/url] |
<?php print(" <a href=\"cart.php?empty=1\">Svuota carrello</a> "); ?>
</body>
</html>