1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php
// Création du panier si n'existe pas dans la session de l'utilisateur
session_start();
include 'Connexionbd.php';
$vendeurpan=$_GET['vend'];
if (! isset($_SESSION['panier'])) $_SESSION['panier'] = array();
// Voici les données externes utilisées par le panier
$id_article = isset($_GET['id_article']) ? $_GET['id_article'] : null;
$nom_article = isset($_GET['nom_article']) ? $_GET['nom_article'] : null;
$prix_article = isset($_GET['prix_article']) ? $_GET['prix_article'] : '?';
$qte_article = isset($_GET['qte_article']) ? $_GET['qte_article'] : 1;
//fonction pour vider
if ($_GET['action'] == "vider") {
$_SESSION['panier'] = array();
}
//fonction pour ajouter 1 a la quantite d'un article deja cree
if ($_GET['action'] == "ajouterqplus") {
$ttc=$_SESSION['panier'][$id_article];
if($ttc != ""){$_SESSION['panier'][$id_article]['qte'] = $_SESSION['panier'][$id_article]['qte']+1;}
if($ttc == ""){
$_SESSION['panier'][$id_article]['vendeur'] = $vendeurpan;
$_SESSION['panier'][$id_article]['nom'] = $nom_article;
$_SESSION['panier'][$id_article]['prix'] = $prix_article;
$_SESSION['panier'][$id_article]['qte'] = 1;
}}
// Voici les traitements du panier
if ($id_article == null) {
//echo 'Veuillez sélectionner un article pour le mettre dans le panier!'; // Message si pas d'acticle sélectionné
}
else
if (isset($_GET['ajouter'])){ // Ajouter un nouvel article
$_SESSION['panier'][$id_article]['vendeur'] = $vendeurpan;
$_SESSION['panier'][$id_article]['nom'] = $nom_article;
$_SESSION['panier'][$id_article]['prix'] = $prix_article;
$_SESSION['panier'][$id_article]['qte'] = $qte_article;
}
else if ($_GET['action']=="modifier") {
$_SESSION['panier'][$id_article]['qte'] = $qte_article;}
else if ($_GET['action']=="supprimer"){ unset($_SESSION['panier'][$id_article]); }// Supprimer un article du panier
// Voici l'affichage du panier
echo '<h2>Contenu de votre panier</h2><ul>';
asort($_SESSION['panier']);
if (isset($_SESSION['panier']) && count($_SESSION['panier'])>0){
$total_panier = 0;
foreach($_SESSION['panier'] as $id_article=>$article_acheté){
echo"$article_acheté[nom] vendu par $article_acheté[vendeur]";
if (isset($article_acheté['nom']) && isset($article_acheté['prix']) && isset($article_acheté['qte'])){
// Calcule le prix total du panier
$total_panier += $article_acheté['prix'] * $article_acheté['qte'];
}
}
echo '<hr><h3>Total: ', number_format($total_panier, 2, ',', ' '), ' ';
echo"+ Frais de port $totalport ";
// Affiche le total du panier
}
else { echo 'Votre panier est vide'; } // Message si le panier est vide
echo "</ul>";
?> |