Salut tout le monde.
J'essaie de mettre en place un scrit de panier qui fonctionne parfaitement, sauf que ce script ne permet pas de définir une quantité. Je peux juste ajouter une fois l'article et c'est tout.
Donc pas terrible...
Le hic c'est que je ne sais pas comment m'y prendre pour adapter le script et rajouter une fonction quantite, se référerant a l'ID de l article.
Voici mon script bdlib.php (fonction):
Ma page qui affiche les produits:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 function get_produits() { global $db; $res = $db -> query("SELECT * FROM produit", array()); $out = array(); if ($res != null) while ($res -> fetchInto($row, DB_FETCHMODE_ASSOC)) {$out[]=$row;} return $out; } function get_quantite($id) { global $db; $res = $db -> query("SELECT * FROM produit WHERE id=?", array($id)); if ($res != null) { $res -> fetchInto($row, DB_FETCHMODE_ASSOC); return $row; } return null; } function produit_info($id) { global $db; $res = $db -> query("SELECT * FROM produit WHERE id=?", array($id)); if ($res != null) { $res -> fetchInto($row, DB_FETCHMODE_ASSOC); return $row; } return null; }
j'ai ajouté un input pour la quantité (<input type="texte" name="quantite[]" size="1" value="" />)
et mon script qui ajoute tout dans le panier
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php session_start(); require_once ("bdlib.php"); $quantite = get_quantite(); $produits = get_produits(); ?> <html> <head> <title>Mes Produits</title> </head> <style type="text/css"> h1 { border-bottom:1px solid black; font-size:medium; margin-bottom:1px } .no_border { border:0px } </style> <script> function acheter (prod_id) { document.getElementById('prod_id').value = prod_id; document.getElementById('acheterform').submit(); return null; } </script> <body> <table width="600px" border="0" cellpadding="5" cellspacing="0"> <tr> <td width="70%" valign="top"> <table width="100%"> <tr> <td width="50%"><strong>Nom</strong></td> <td width="30%" align="center"><strong>Prix</strong></td> <td width="10%" align="center"><strong>Quantité</strong></td> <td width="10%" align="center"> </td> </tr> </table> <table width="100%"> <?php foreach($produits as $produit) { ?> <tr> <td width="50%"><?php echo($produit['nom']); ?></td> <td width="30%" align="center"><?php echo($produit['prix']); ?> Frs </td> <td width="10%" align="right"> <form id="acheterform" action="ajouter.php" method="post"> <input type="hidden" name="prod_id" id="prod_id" value="" /> <input type="texte" name="quantite[]" size="1" value="" /> </form> </td> <td width="10%" align="center"><a href="javascript:acheter(<?php echo($produit['id']); ?>);"><img src="img/basket.gif" class="no_border"/></a> </tr> <?php } ?> </table> </td> <td width="30%" valign="top"> <h1>Panier d'achat</h1> <?php if(isset($_SESSION['panier'])) { ?> <!--PANIER : <?php echo(join(",",array_keys($_SESSION['panier']))); ?> --> <table width="100%" cellpadding="5" cellspacing="0"> <?php foreach(array_keys($_SESSION['panier']) as $produit) { $info = produit_info($produit); ?> <tr><td> <?php echo($info['nom']); ?> <?php echo($_POST['quantite']); ?> </td></tr> <?php } ?> <tr><td align="center"> <a href="valider.php">Valider le paiement</a> </td></tr> </table> <?php } ?> </td></td> </table> </body> </html>
La j'ai ajouté la variable $_POST['panier']
Merci d'avance pour votre aide ou vos explications et bon dév
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <?php session_start(); if (!isset($_SESSION['panier'])) $_SESSION['panier'] = array(); $_SESSION['panier'][$_POST['prod_id']] [$_POST['quantite']] = 1; header ("location: produits.php"); ?>
Partager