-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanier.php
More file actions
121 lines (116 loc) · 4.5 KB
/
Copy pathpanier.php
File metadata and controls
121 lines (116 loc) · 4.5 KB
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
<?php
$titre="Panier";
include_once('inc/header.php');
$db=new db;
if(!isset($_SESSION['user'])){ // si c'est un client connecté
if(isset($_COOKIE['token'])){
$q="SELECT * FROM users where token='".$db->verifyAndReturn($_COOKIE['token'])."'";
$result=$db->returnData($q,'one');
if(empty($result)){
header('Location: login');
exit;
}else{
$_SESSION['user']=$result['username']; //on recupere le nom du client
$_SESSION['id']=$result['id']; //on recupere l'ID du client
}
}else{ // si n'est pas un client
$_SESSION['visiteur']="v";
header('Location: login'); //redirection vers la page de login
exit;
}
}
if(!isset($_SESSION['id'])) $_SESSION['id']=$db->returnData("SELECT id FROM users where token='".$db->verifyAndReturn($_COOKIE['token'])."'",'one')['id'];
//ajout d'un produit au panier si le client a cliqué sur ajouter au panier
if(isset($_POST['submit'])){
try{
$q="insert into panier values(NULL,?,?,?,?)";
$data=explode(',',$_POST['data']);
array_push($data, $_SESSION['id']);
if($db->insertData($q, $data)){
$Response="Produit ajouter au panier avec success";
$alert_type="success";
}else{
$Response="Produit n'est pas ajouter";
$alert_type="danger";
}
}catch(Exception $e){
$Response=$e->getMessage();
$alert_type="danger";
}
}
include_once('inc/nav.php');
//suppression d'un produit du panier
if(isset($_POST['delete'])){
$db->deleteData("delete from panier where id='".$db->verifyAndReturn($_POST['id'])."'");
}
?>
<div class="container content mt-2">
<?php if(isset($Response)){ ?>
<div class="mt-2 alert alert-<?= $alert_type.'">'.$Response; ?></div>
<?php } ?>
<table class="table table-striped mt-3 mb-5">
<thead class="thead-dark">
<tr>
<th scope="col-sm-1">Id</th>
<th scope="col">Titre</th>
<th scope="col">Prix(DH)</th>
<th scope="col-sm-3">Image</th>
<th scope="col-sm-3">quantité</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php
if(!isset($_SESSION['id'])){
$q="select id from users where username='".$_SESSION['user']."'";
$_SESSION['id']=$db->returnData($q,'one')['id'];
}
$q="SELECT *,count(*) as quantite,sum(prix) as somme FROM panier where user_id='".$_SESSION['id']."' group by title";// on recupere tous les produits ajouté par ce client à son panier
$arr=$db->returnData($q,'many');
if(!$arr){ ?> <!-- si le panier est vide -->
<tr>
<td colspan=6>
<div class="lead text-center">Pas de Produit Actuelement
<a class="btn btn-dark" href="index">Ajouter Des Produits</a>
</div>
</td>
</tr>
<?php }else{ //sinon on afficher l'ensemble des produits ajoutés par le client
$somme=0;
foreach($arr as $element){
$somme+=$element['quantite']*$element['prix'];
$p_id=$db->returnData("select id from produits where title='".$element['title']."'",'one')['id'];
?>
<tr>
<th scope="row"><?=$element['id']; ?></th>
<td><?=$element['title']; ?></td>
<td><?=$element['prix']; ?></td>
<td>
<img src="<?=$element['image']; ?>" class="img-thumbnail">
</td>
<td><?=$element['quantite']; ?></td>
<td>
<div class="btn-group">
<a href="produit?p=<?=$p_id?>"><button class="pr-3 btn btn-warning"><i class="fas fa-info-circle"></i></button></a>
<form method=post action=panier>
<input type=hidden name=id value=<?=$element['id']; ?>>
<button type="submit" name='delete' class="pl-3 btn btn-danger"><i class="fas fa-trash"></i></button>
</form>
</div>
</td>
</tr>
<?php } ?>
<tr>
<td colspan=4></td>
<td>
<div class="lead text-center text-primary"><b>Total est: $<?=$somme;?></b></div>
</td>
<td>
<a href="/checkout"><button class="btn btn-primary">checkout</button></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php include_once('inc/footer.php'); ?>