<?php
    session_start
();
    
    class 
LigneProduit {
       public 
$nom ;
       public 
$qte ;
    
       function 
__construct($nom) {
         
$this->nom $nom;
         
$this->qte 1
       }
    }
    
    
/* on ajoute un produit au panier */
    
function ajouterProduit($produit) {
       
$qte 0;
       
       if ( !isset(
$_SESSION[$produit]) ) { //produit n'est pas encore au panier
          
$_SESSION[$produit] = new LigneProduit($produit);
          
$qte $_SESSION[$produit]->qte //on recupere la quantite
       
}
       else { 
//on avait deja ajoute ce produit, augmenter alors sa quantite 
          
$objet $_SESSION[$produit] ;
          
$objet->qte $objet->qte 1;
          
$qte $objet->qte //on recupere la quantite
       
}
       
       return 
$qte;
    }
    
    
/* on supprime un produit du panier */
    
function supprimerProduit($produit) {
       
$qte ;
       
       if ( isset(
$_SESSION[$produit]) ) { //on recupere le produit
          
$objet $_SESSION[$produit] ;
          
$objet->qte $objet->qte 1;
          
          
$qte $objet->qte
                
          if ( 
$qte <= 0) { //il ne reste plus rien du produit dans le panier
             
unset($_SESSION[$produit]);
          }
       }
       
       return 
$qte;
    }
    
    
/* affichage du contenu du panier */
    
function afficherPanier() {
       echo 
"<table>" ;
       foreach(
$_SESSION as $objet) {
          echo 
"<tr><td> " $objet->nom " </td> <td> "$objet->qte " </td> </tr> ";
        } 
        echo 
"</table>" ;
    }
    
    
/* fermeture de la session */
    
function terminerSession() {
       unset(
$_SESSION);
       
session_destroy();
    }
?>
    
<!DOCTYPE html>
<html>
 <head>
     <meta charset="UTF-8" />
     <meta name="author" content="Manuele" />
    <title>Sessions en PHP</title>
    <link rel="stylesheet" href="css/panier.css" />
 </head>
 <body>
   <header> <h1>Exemple de Panier</h1> </header>
   <nav>
      <p> <a href="formPanier.html"> Produits </a> </p>
         <p> <a href="Panier.php?action=terminer"> Terminer </a></p>
         <p> <a href="Panier.php"> Voir panier </a> <p>
    </nav>
    
   <section> 
     <article>
     <?php 
       
if( ! empty ( $_GET["action"] ) AND ! empty ( $_GET["produit"] ) ) {
          
$action $_GET["action"];
          
$produit $_GET["produit"];  
          
          
/* on veut ajouter un nouveau produit */
          
if ($action == "ajouter") {
             
$qte ajouterProduit($produit) ;
             
             echo 
"<p>Ajouter $produit au panier :  quantite $qte </p>" ;
          }
          
/* on veut supprimer un produit */
          
elseif ($action == "supprimer" ) {
             
$qte supprimerProduit($produit) ;
             if (
$qte 0) {
                echo 
"<p>Suppression produit $produit : quantite actuelle $qte </p>" ;
             }
             else {
                echo 
"<p>Produit $produit supprimé du panier </p>" ;
             }
          }
          
          
afficherPanier();
        }
       else {
          
/* si on ne veut ni ajouter, ni supprimer, 
            soit on veut consulter le panier, soit on veut terminer la session */
          
          //on veut terminer
          
if ( ! empty ( $_GET["action"] ) ) {
              
terminerSession();
              echo 
"<p>Bye bye...</p>";
          }
          else {
              echo 
"<p>Le panier contient " count($_SESSION) . " produits. </p>" ;
              
afficherPanier();
          }
       } 
   
?>      
    </article> 
   </section> 
 </body>
</html>