-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanier.php
128 lines (111 loc) · 4.68 KB
/
panier.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php session_start(); ?>
<!DOCTYPE html>
<head>
<meta charset="UTF-8"/>
<title>MineTek</title>
<link rel="stylesheet" href="style_panier.css" media="all" />
<link rel="shortcut icon" href="Images/logo_blue_onglet.ico">
</head>
<?php include 'header.php'; ?>
<section class="intro">
<h1 class="panier"> PANIER </h1>
<div class="content">
<?php
function get_nb_produits_panier(){ //fonction qui compte le nombre de produits actuellement dans le panier
$total=0;
for($i = 0; $i < count($_SESSION['panier']['produit']); $i++)
{
$total += $_SESSION['panier']['qteProduit'][$i];
}
return $total;
}
if(!isset($_SESSION['panier'])){ //creation du panier sous forme de tableau
$_SESSION['panier'] = array();
$_SESSION['panier']['produit'] = array();
$_SESSION['panier']['id'] = array();
$_SESSION['panier']['qteProduit'] = array();
}
$titrePage = "Panier";
require 'config.php';
// Si on demande de vider le panier
if(isset($_GET['vider'])){
$_SESSION['panier'] = array();
$_SESSION['panier']['produit'] = array();
$_SESSION['panier']['id'] = array();
$_SESSION['panier']['qteProduit'] = array();
}
if(isset($_GET['supprimer'])){
$sup = $_GET['supprimer'];
// On vérifie que l'item que l'on souhaite supprimer soit dans l'array
if(in_array($sup, $_SESSION['panier']['id'])){
// On cherche sa position
$pos = array_search($sup, $_SESSION['panier']['id']);
//On vérifie qu'il y a plus d'un item à supprimer
if($_SESSION['panier']['qteProduit'][$pos] > 1){
$_SESSION['panier']['qteProduit'][$pos]--;
}
else{
// Sinon, on supprimer la ligne des arrays
unset($_SESSION['panier']['qteProduit'][$pos]);
unset($_SESSION['panier']['id'][$pos]);
unset($_SESSION['panier']['produit'][$pos]);
}
}
}
if(isset($_GET['ajouter'])){
// L'id du jeu à ajouter doit être un nombre
if(is_numeric($_GET['ajouter'])){
// Le panier ne doit pas contenir moins de 3 articles
if(get_nb_produits_panier() >= 3){
echo "<h3>Impossible d'ajouter ce produit : vous possédez déjà 3 articles dans votre panier.</h3>";
}
else{
$id = $_GET['ajouter'];
//Si le produit que l'on veut ajouter est déjà dans le panier, on change sa quantité
if(in_array($id,$_SESSION['panier']['id'])){
$_SESSION['panier']['qteProduit'][array_search($id, $_SESSION['panier']['id'])]++;
}
else{
$requete = mysql_query('SELECT * FROM FC_grp3_Jeux WHERE indice = "' . $id . '"');
if(!$requete) {
die('Erreur dans la requête : ' . mysql_error());
}
$valeur = mysql_fetch_array($requete);
// L'id est présent dans la liste des jeux
if(!empty($valeur)){
// Le jeu doit être en stock
if($valeur["QuantitePrdt"] > 0 and date("Y-m-d") > $valeur['date']){
// On ajoute le jeu à l'array
array_push($_SESSION['panier']['produit'], $valeur['Nom']);
array_push($_SESSION['panier']['id'], $valeur['indice']);
array_push($_SESSION['panier']['qteProduit'], "1");
}
else{
echo "<h3>Le jeu demandé n'est plus en stock.</h3>";
}
}
}
}
}
}
if(empty($_SESSION['panier']['produit'])){
echo "Votre panier est vide.";
}
else{
echo "<p><a class='modif_panier' href='?vider'>Vider le panier</a></p>";
echo "<table id='table_panier'>\n\n";
echo "<tr><th>Produit</th><th>Quantité</th><th></th></tr>\n";
foreach ($_SESSION['panier']['produit'] as $id => $libelle) {
echo "\n<tr>\n";
echo "<td>" . $_SESSION['panier']['produit'][$id] . "</td>\n";
echo "<td>" . $_SESSION['panier']['qteProduit'][$id] . "</td>\n";
echo "<td><a class='modif_panier' href='?supprimer=" . $_SESSION['panier']['id'][$id] . "'>-</a><a class='modif_panier' href='?ajouter=" . $_SESSION['panier']['id'][$id] . "'>+</a></td>\n";
echo "</tr>\n";
}
echo "\n\n</table>";
echo '<div id="reserver_link"><a href="reserver.php" id="reserver_ecrit">Réserver</a></div>';
}
?>
</div>
</section>
<?php include 'footer.php'; ?>