Bonjour,
je suis en trai de créer un panier avec utilisation des sessions.
J'ai une première page (index.php) qui correspond à mon catalogue. Chaque article a son propre bouton ajouter. Lorsque je clique sur celui ci, je passe sur la page panier (cart.php) qui liste mon panier. Le problème est que lorsque je clique sur le lien pour revenir sur le catalogue (afin de choisir un autre article), mon panier se vide est ne laisse apparaitre que le dernier article choisi.
Voici mes deux pages :
index.php
<?php
if (!$_SESSION['panier'])
session_start($_SESSION['panier']);
?>
<!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>
et 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<?php SID; ?>">Articles</a> | <a href="invoice.php">Paiement</a></td>
</table>
<?php
$_SESSION['article'] = $_POST['article'];
$_SESSION['code'] = $_POST['code'];
$_SESSION['qte'] = $_POST['qte'];
$_SESSION['prix'] = $_POST['prix'];
$_SESSION['tva'] = $_POST['tva'];
$_SESSION['poids'] = $_POST['poids'];
$total = $_SESSION['prix'] * $_SESSION['qte'];
$nbArticles=count($_SESSION['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++)
{
echo '<tr>';
echo '<td class="Style3" width="16%">';
echo $_SESSION['code'];
echo '</td>';
echo '<td class="Style31" width="16%">';
echo $_SESSION['article'];
echo '</td>';
echo '<td class="Style3" width="16%">';
echo '<img src="images/moins.gif"> ';
echo $_SESSION['qte'];
echo ' <img src="images/plus.gif">';
echo '</td>';
echo '<td class="Style3" width="16%">';
echo $_SESSION['prix'];
echo '</td>';
echo '<td class="Style3" width="16%">';
echo $total;
echo '</td>';
echo '<td class="Style3" width="16%">';
echo ' ';
echo '</td>';
echo '</tr>';
}
}
?>
</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%"><a href="?vide=ok">Vider</a></td>
</tr>
</table>
</fieldset>
</body>
</html>
Merci de m'aider.