je suis en train de créer un panier à l'aide de sessions. J'ai une première page catalogue (index.php) où sont listés mes articles. Chacun d'eux a un bouton submit pour l'ajouter au panier.
Ma deuxième page (cart.php) doit lister tous les articles de mon panier. J'y ai aussi un lien pour revenir à la page catalogue.
Mon problème vient des sessions : je n'arrive pas à ajouter plusieurs articles au panier. Je ne visualise que le dernier article choisi.
code de ma page index.php :
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Catalogue</title>
<link rel="stylesheet" href="fichier.css" type="text/css">
</head>
<body>
<table width="100%">
<tr>
<td class="Style1"><p></p><p>Ma boutique en ligne.</p></td>
</tr>
<tr>
<td class="Style2">Panier ( )</td>
</table>
<fieldset>
<legend align="left">
<span id="cadre">Articles</span>
</legend>
<table width="100%">
<tr>
<td>
<table class="Style3">
<tr>
<td colspan="2" class="Style4">Canard de bain</td>
</tr>
<tr>
<td><img src="images/canard.gif" title="Canard de bain" height="100" width="100"></td>
<td><p>Un compagnon de jeu pour le bain.</p>
<p><b>2.99 Eur</b></p>
<p><form method="POST" action="cart.php">
<input type="hidden" name="code" value="ART001">
<input type="hidden" name="article" value="canard">
<input type="hidden" name="qte" value="1">
<input type="hidden" name="prix" value="2.99">
<input type="hidden" name="tva" value="19.6">
<input type="hidden" name="poids" value="0.300">
<input type="submit" name="ajouter" value="ajouter"></form></p>
</td>
</tr>
</table>
</td>
<td>
<table class="Style3">
<tr>
<td colspan="2" class="Style4">Compas</td>
</tr>
<tr>
<td><img src="images/compas.jpg" title="Compas" height="100" width="100"></td>
<td><p>Pour ne pas perdre le nord.</p>
<p><b>9.95 Eur</b></p>
<p><form method="POST" action="cart.php">
<input type="hidden" name="code" value="ART002">
<input type="hidden" name="article" value="Compas">
<input type="hidden" name="qte" value="1">
<input type="hidden" name="prix" value="9.95">
<input type="hidden" name="tva" value="19.6">
<input type="hidden" name="poids" value="0.900">
<input type="submit" name="ajouter" value="ajouter"></form></p>
</td>
</tr>
</table>
</td>
<td>
<table class="Style3">
<tr>
<td colspan="2" class="Style4">Aquarium et poisson rouge</td>
</tr>
<tr>
<td><img src="images/aquarium.jpg" title="Aquarium" height="100" width="100"></td>
<td><p>Pour passer le temps.</p>
<p><b>24.95 Eur</b></p>
<p><form method="POST" action="cart.php">
<input type="hidden" name="code" value="ART003">
<input type="hidden" name="article" value="Aquarium">
<input type="hidden" name="qte" value="1">
<input type="hidden" name="prix" value="24.95">
<input type="hidden" name="tva" value="19.6">
<input type="hidden" name="poids" value="3.500">
<input type="submit" name="aquarium" value="ajouter"></form></p>
</td>
</tr>
</table>
</td>
<td>
<table class="Style3">
<tr>
<td colspan="2" class="Style4">Boite à lettres</td>
</tr>
<tr>
<td><img src="images/boite.gif" title="Boite à lettres" height="100" width="100"></td>
<td><p>Vous avez du courrier.</p>
<p><b>12.99 Eur</b></p>
<p><form method="POST" action="cart.php">
<input type="hidden" name="code" value="ART004">
<input type="hidden" name="article" value="Boite à lettres">
<input type="hidden" name="qte" value="1">
<input type="hidden" name="prix" value="12.99">
<input type="hidden" name="tva" value="19.6">
<input type="hidden" name="poids" value="5.000">
<input type="submit" name="boite" value="ajouter"></form></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</fieldset>
<p>(c) 2008 - Ma Boutique en ligne - 1.0.0</p>
</body>
</html>
/////////////////////////////
code de ma page cart.php :
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Panier</title>
<link rel="stylesheet" href="fichier.css" type="text/css">
</head>
<body>
<table width="100%">
<tr>
<td class="Style1"><p></p><p>Ma boutique en ligne.</p></td>
</tr>
<tr>
<td class="Style2"><a href="index.php">Articles</a> | <a href="invoice.php">Paiement</a></td>
</table>
<?php
$_SESSION['panier'] = $panier;
$panier['article'] = $_POST['article'];
$panier['code'] = $_POST['code'];
$panier['qte'] = $_POST['qte'];
$panier['prix'] = $_POST['prix'];
$panier['tva'] = $_POST['tva'];
$panier['poids'] = $_POST['poids'];
$total = $panier['prix'] * $panier['qte'];
$nbArticles=count($panier['article']);
if ($nbArticles <= 0)
echo "<tr><td>Votre panier est vide </ td></tr>";
else
{
?>
<fieldset>
<legend align="left">
<span id="cadre">Panier</span>
</legend>
<table width="100%">
<tr>
<td class="Style11" width="16%">Code article</td>
<td class="Style11" width="16%">Produit</td>
<td class="Style11" width="16%">Quantité</td>
<td class="Style11" width="16%">Prix</td>
<td class="Style11" width="16%">Total</td>
<td class="Style11" width="16%"></td>
</tr>
<?php
for ($i=0 ;$i < $nbArticles ; $i++)
{
?>
<tr>
<td class="Style3" width="16%"><?php echo $panier['code'];?></td>
<td class="Style31" width="16%"><?php echo $panier['article'];?></td>
<td class="Style3" width="16%"><img src="images/moins.gif"> <?php echo $panier['qte'];?> <img src="images/plus.gif"></td>
<td class="Style3" width="16%"><?php echo $panier['prix'];?></td>
<td class="Style3" width="16%"><?php echo $total;?></td>
<td class="Style3" width="16%"></td>
</tr>
<?php
}
}
?>
</table>
<table width="100%">
<tr>
<td class="Style32" width="16%"></td>
<td class="Style32" width="16%"></td>
<td class="Style32" width="16%"></td>
<td class="Style32" width="16%">TOTAL</td>
<td class="Style32" width="16%">
<?php
$total2 = '12';
echo $total2;
?>
</td>
<td class="Style32" width="16%">
<form method="post" action="vider.php"><input type="button" value="vider" name="vider"></form>
</td>
</tr>
</fieldset>
</body>
</html>
////////////
d'où vient mon problème, pourquoi ne puis-je pas ajouter de nouveau article? Je tourne en rond, d'habitude je bosse avec des bases de données, alors les sessions, je ne connais pas trop, et j'ai beau faire des recherches sur le net, je n'y arrive pas.
Merci de votre aide.