Code :
- <?php
- /*
- NOTES / explications :
- variables panier sous la forme : $_SESSION['panier'][reference][array(designation=>blabla,prixht=>blabla,quantite=>blabla);
-
- Anti reload :
- Afin de protéger le panier d'une incrémentation lors d'un reload de la page ou d'un prec/suiv, chaque chargement du formulaire
- est identifié avec un ID unique ($_POST['panier_lock']) qui est également enregistré dans une variable de session ($_SESSION['panier_lock'])
- Ces deux variables sont ensuite comparées à l'arrivée sur la page panier. Si elles sont identiques, l'article est ajouté, puis la variable
- de session est détruite en fin de script.
- */
- session_start();
- $tot_ht=0; // montant total HT
- if (!isset($_SESSION['panier'])) $_SESSION['panier']=array();
- if (isset($_POST['ref']) && isset($_SESSION['panier_lock']) && isset($_POST['panier_lock'])) { // si il s'agit de l'ajout d'un article et que la page n'a pas déjà été appellée
- // un peu de sécurité
- $prixht=$_POST['prixht'];
- settype($prixht,"float" );
- $ref=strip_tags($_POST['ref']);
- $quantite=$_POST['quantite'];
- settype($quantite,"int" );
- if ($prixht==0) $quantite=0;
-
- // protection reload
- if ($_POST['panier_lock']===$_SESSION['panier_lock']) {
- $panier_lock=true; // autorise la modification du panier
- } else {
- $panier_lock=false; // interdit la modification du panier
- }
-
- if ($panier_lock==true) { // autorisation ajout panier
- if (array_key_exists($ref, $_SESSION['panier'])) { // la ref existe déjà dans le panier
- $_SESSION['panier'][$ref]['quantite']+=$_POST['quantite']; // ajout de la quantité à celle existante pour le même article
- } else { // la ref n'est pas encore présente dans le panier, on l'ajoute
- $_SESSION['panier'][$ref]['designation'] = $_POST['designation'];
- $_SESSION['panier'][$ref]['prixht'] = $prixht;
- $_SESSION['panier'][$ref]['quantite'] = $_POST['quantite'];
- }
- }
- }
- if (count($_SESSION['panier'])) { // si le panier n'est pas vide
- echo'
- <style type="text/css">
- <!--
- body,td,th {
- color: #006699;
- }
- body {
- background-image: url(../design/styles/fond.gif);
- }
- -->
- </style>
- <link href="../design/styles/styles.css" rel="stylesheet" type="text/css">
- <table width="100%" border="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
- <tr bordercolor="#BDDFE1" bgcolor="#BDDFE1" class="menugauche">
- <td width="20%">Reference</td>
- <td width="20%" bordercolor="#BDDFE1">Description</td>
- <td width="20%">Quantité</td>
- <td width="20%">Prix HT € unitaire </td>
- <td width="20%">Total HT € </td>
- </tr>
- </table>
- <table width="100%" border="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF" >
- ';
-
- foreach($_SESSION['panier'] as $key => $value) { // pour chaque éléments du panier
- echo '<tr>
- <td width="20%">'.$key.'</td>
- <td width="20%">'.$_SESSION['panier'][$key]['designation'].'</td>
- <td width="20%">'.$_SESSION['panier'][$key]['quantite'].'</td>
- <td width="20%">'.$_SESSION['panier'][$key]['prixht'].'</td>';
-
- $tot_line=$_SESSION['panier'][$key]['quantite'] * $_SESSION['panier'][$key]['prixht']; // total HT / ref
- $tot_ht +=$tot_line; // calcul le montant total HT du panier
-
- echo '<td width="20%">'.number_format(round($tot_line,2), 2, '.', ' ').'</td>
- </tr>';
- }
- echo '<tr><td colspan="4">Montant total HT de votre commande (€)</td><td width="13%">'.number_format(round($tot_ht,2), 2,'.', ' ').' €</td></tr></table>';
- } else {
- echo 'Votre panier ne contient aucun article.'; // le client n'a pas d'article dans son panier
- }
- unset($_SESSION['panier_lock']); // enclenche le verrouillage
- ?>
|