-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajouter_panier.php
More file actions
40 lines (31 loc) · 1001 Bytes
/
ajouter_panier.php
File metadata and controls
40 lines (31 loc) · 1001 Bytes
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
<?php
session_start();
if(empty($_GET['id'])){
header('Location: index.php');
exit;
}
// $_GET c'est le tableau associatif qui est passé dans l'url : 2 vaudra en réalité "2"
// (int) : est un CAST ou CONVERSION DE TYPE en PHP
$id = (int) $_GET['id'];
// Récupérer la liste des produits
require_once('produits.php');
// Avant d'ajouter au panier, on vérifie si le produit existe
if(!isset($produits[$id])){
header('Location: index.php');
exit;
}
// Initialiser le panier s'il n'existe pas déjà
if(!isset($_SESSION["panier"])){
$_SESSION['panier'] = [];
}
// Le produit n'existe pas dans le panier > On en ajoute
// Le produit existe déjà dans le panier > On récupère le nombre et on ajoute
if(!isset($_SESSION['panier'][$id])){
$_SESSION['panier'][$id] = 1;
} else {
// Le produit est déjà présent dans le panier, on ajoute 1
$_SESSION['panier'][$id]++;
}
// Redirection vers la page panier ou vers la page produit
header('Location: panier.php');
exit;