Précédent   Forum des professionnels en informatique > PHP > Langage > Formulaires
Formulaires Forum d'entraide sur les formulaires avec PHP. Avant de poster -> FAQ formulaires, Cours de formulaires et Sources de formulaires
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 26/03/2011, 19h16   #1
Invité de passage
 
Homme
Développeur Web
Inscription : mars 2011
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : mars 2011
Messages : 4
Points : 0
Points : 0
Par défaut Récupération de variable + tableau associatif

Bonjour,

Je rencontre une difficulté par rapport à un panier, dans mon panier doit figurer la taille du vêtement choisi par l'internaute cela fonctionne mais quand je prends un autre article d'une taille différente le panier m'affiche pour tous les articles la même taille.

Que dois-je faire ?
am.hichame est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/03/2011, 20h24   #2
Membre émérite
 
Avatar de vorace
 
Homme
Développeur
Inscription : août 2010
Messages : 587
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Meurthe et Moselle (Lorraine)

Informations professionnelles :
Activité : Développeur

Informations forums :
Inscription : août 2010
Messages : 587
Points : 861
Points : 861
nous montrer le code problématique...
__________________
Développeur informatique contrarié...
vorace est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/03/2011, 20h45   #3
Invité de passage
 
Homme
Développeur Web
Inscription : mars 2011
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : mars 2011
Messages : 4
Points : 0
Points : 0
Code :
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?
session_start();
 
if (isset($_SESSION["caddie"])) 
{
$caddie = $_SESSION["caddie"];
} 
else 
{
$caddie = array();
}
 
if (isset($sup))
{
unset($caddie["$sup"]);
}
 
if (isset($lettre))
{
$sizel = $lettre;
}
 
if (empty($_POST['pnum']))
{
$quantite=1;
}
else
{
$quantite=$_POST['pnum'];
}
 
$caddie[$article]=$caddie[$article]+$quantite;
 
$i=0;
if (isset($nombre[$i]))
{ 
$a=count($_SESSION["caddie"]);
$a--;
for ($j=0;$j<$a;$j++)
{
if ((ereg("^([0-9]+)$",$nombre[$j])) && $nombre[$j]!="0")
{
$caddie["$produit[$j]"]=$nombre[$j];
}
}
}
 
include("design/headeri.php");
require("config.php");
 
$_SESSION["caddie"] = $caddie;
 
echo"<td bgcolor=\"$barre1\"><a href=\"index.php\"><font size=\"2px\"<strong>Accueil</a> > <a href=\"liste.php?cat=1\">Homme</a> > Panier </strong></font><br><br><br></td>
  </tr>
  <tr><br>";
 
echo"<br><center><table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"$bordercolor\">
  <tr>
    <td><div align=\"center\"><table width=\"70%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"$barre1\">
  <tr>
    <td width=\"30%\"><div align=\"center\"><strong><font color=\"$police3\" size=\"$width_police2\">Designation</font></strong></div></td>
    <td width=\"10%\"><div align=\"center\"><strong><font color=\"$police3\" size=\"$width_police2\">Taille</font></strong></div></td>
    <td width=\"10%\"><div align=\"center\"><strong><font color=\"$police3\" size=\"$width_police2\">Prix unitaire</font></strong></div></td>
    <td width=\"5%\"><div align=\"center\"><strong><font color=\"$police3\" size=\"$width_police2\">Quantité</font></strong></div></td>
    <td width=\"10%\"><div align=\"center\"><strong><font color=\"$police3\" size=\"$width_police2\">Prix total</font></strong></div></td>
    <td width=\"5%\"><div align=\"center\"><strong><font color=\"$police3\" size=\"$width_police2\">Supprimer</font></strong></div></td>
  </tr>";
 
 
while($var = each($caddie))
{
$sql = "SELECT id_pro,titre_prod,pxht_prod,promo_prod,pxhtpromo_prod  FROM $table_produit where id_pro='$var[0]'";
$sql = mysql_db_query($base,$sql);
 
while ($site=mysql_fetch_array($sql))
{
if (empty($nombre[$i]) || !(ereg("^([0-9]+)$",$nombre[$j])) || $nombre[$j]=="0")
{
$nombre[$i]="$var[1]";
}
else
{
$var[1]=$nombre[$i]; 
}
 
if ($site[promo_prod]=="1")
{
$site[pxht_prod]=$site[pxhtpromo_prod];
}
 
$pxtotal=$site[pxht_prod]*$var[1];
$total=$total+($site[pxht_prod]*$var[1]);
 
echo"<tr bgcolor=\"$barre3\">
    <td><div align=\"center\">$site[titre_prod]</div></td>
    <td><div align=\"center\">$silzel</div></td>
    <td><div align=\"center\">$site[pxht_prod] €</div></td>
    <td><div align=\"center\"><form action=\"panier.php\"  method=\"post\" style='margin-top: 0px; margin-bottom: 0px'><input name=\"produit[$i]\" type=\"hidden\" value=\"$var[0]\"><input name=\"nombre[$i]\" type=\"text\" value=\"$nombre[$i]\" size=\"2\"></div><br></td>
    <td><div align=\"center\">$pxtotal €</div></td>
    <td><div align=\"center\"><a href=\"panier.php?sup=$site[id_pro]\"><img src=\"design/suprimmer.gif\" border=\"0\"onclick=\"javascript:if(!confirm('Etes-vous sûr de vouloir supprimer cet article de votre panier?')) return false;\" ></a></div></td>
  </tr>";
 
$i++;
$test="1";
}
}
 
if ($test=="1")
{
echo"<tr bgcolor=\"$barre3\">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><div align=\"center\"><input type=\"image\" src=\"design/recalculer.gif\" border=0></td>
    <td><div align=\"center\"><strong>Total</strong></div></td>
    <td><div align=\"center\"><strong>$total €</strong></div></td>
    <td></form></td>
  </tr>";
}
 
echo"</div></table></td>
  </tr>
</table></center><br><br>";
 
if ($test=="1")
{
echo"<center><table border=\"0\" cellpadding=10>
  <tr>
    <td><a href=\"index.php\"><img src=\"design/poursuis.gif\" border=\"0\"></a></td>
    <td><form action=\"compte.php\" method=\"post\" style='margin-top: 0px; margin-bottom: 0px'>
		<input name=\"achat\" type=\"hidden\" value=\"autorisation\">
		<input  type=\"image\" src=\"design/commande.gif\">
		</form></td>
  </tr>
</table></center><br><br>";
}
else
{
session_unregister("achat");
echo"<center><strong>votre panier est vide</strong><br><br><a href=\"liste.php?cat=1\"><img src=\"design/retour.gif\" border=\"0\"></a><br><br></center>";
}
 
 
include("design/footer.php");
?>
am.hichame est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/03/2011, 22h07   #4
Membre émérite
 
Avatar de vorace
 
Homme
Développeur
Inscription : août 2010
Messages : 587
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Meurthe et Moselle (Lorraine)

Informations professionnelles :
Activité : Développeur

Informations forums :
Inscription : août 2010
Messages : 587
Points : 861
Points : 861
tu as une erreur ici je crois :
Code :
<td><div align=\"center\">$silzel</div></td>
c'est pas $sizel ?
Tu fais chevaucher des balises, tu fermes plusieurs balises avant de fermer ton formulaire...
__________________
Développeur informatique contrarié...
vorace est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 27/03/2011, 14h30   #5
Invité de passage
 
Homme
Développeur Web
Inscription : mars 2011
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : mars 2011
Messages : 4
Points : 0
Points : 0
Merci Vorace, effectivement une erreur d'étourdi comme d'habitude maintenant si je veux associer la taille choisi par le client avec l'article choisi qu'est je dois faire précisément ?
Avec le code actuel la taille s'affiche dans mon panier mais quand je prend un autre article avec une taille différente, le panier affiche tous les articles du panier avec la dernière taille choisie ?
Je pense que je dois créer un tableau associatif mais je sais pas comment le représenter.
am.hichame est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/03/2011, 14h38   #6
Membre émérite
 
Avatar de vorace
 
Homme
Développeur
Inscription : août 2010
Messages : 587
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Meurthe et Moselle (Lorraine)

Informations professionnelles :
Activité : Développeur

Informations forums :
Inscription : août 2010
Messages : 587
Points : 861
Points : 861
personnellement je me suis servi de ce tuto pour la création de mon panier :
http://jcrozier.developpez.com/articles/web/panier/
__________________
Développeur informatique contrarié...
vorace est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/03/2011, 15h08   #7
Invité de passage
 
Homme
Développeur Web
Inscription : mars 2011
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : mars 2011
Messages : 4
Points : 0
Points : 0
oui je le connais bien ce tutoriel mais il n'indique pas comment faire pour mon cas et ça me soule parce que je suis dessus depuis quelques jours
am.hichame est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/03/2011, 15h33   #8
Membre émérite
 
Avatar de vorace
 
Homme
Développeur
Inscription : août 2010
Messages : 587
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Meurthe et Moselle (Lorraine)

Informations professionnelles :
Activité : Développeur

Informations forums :
Inscription : août 2010
Messages : 587
Points : 861
Points : 861
voila le même code mais avec un champs supplémentaire pour la taille :
fonctions-panier.php :
Code :
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
 
/**
 * Verifie si le panier existe, le créé sinon
 * @return booleen
 */
function creationPanier(){
   if (!isset($_SESSION['panier'])){
      $_SESSION['panier']=array();
      $_SESSION['panier']['libelleProduit'] = array();
      $_SESSION['panier']['qteProduit'] = array();
      $_SESSION['panier']['prixProduit'] = array();
	  $_SESSION['panier']['tailleProduit'] = array();//champs suplementaire pour la taille
      $_SESSION['panier']['verrou'] = false;
   }
   return true;
}
 
 
/**
 * Ajoute un article dans le panier
 * @param string $libelleProduit
 * @param int $qteProduit
 * @param float $prixProduit
 * @return void
 */
function ajouterArticle($libelleProduit,$qteProduit,$prixProduit,$tailleProduit){
 
   //Si le panier existe
   if (creationPanier() && !isVerrouille())
   {
      //Si le produit existe déjà on ajoute seulement la quantité
      $positionProduit = array_search($libelleProduit,  $_SESSION['panier']['libelleProduit']);
 
      if ($positionProduit !== false)
      {
         $_SESSION['panier']['qteProduit'][$positionProduit] += $qteProduit ;
      }
      else
      {
         //Sinon on ajoute le produit
         array_push( $_SESSION['panier']['libelleProduit'],$libelleProduit);
         array_push( $_SESSION['panier']['qteProduit'],$qteProduit);
         array_push( $_SESSION['panier']['prixProduit'],$prixProduit);
		 array_push( $_SESSION['panier']['tailleProduit'],$tailleProduit);//ajout de la taille
      }
   }
   else
   echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
 
 
 
/**
 * Modifie la quantité d'un article
 * @param $libelleProduit
 * @param $qteProduit
 * @return void
 */
function modifierQTeArticle($libelleProduit,$qteProduit){
   //Si le panier éxiste
   if (creationPanier() && !isVerrouille())
   {
      //Si la quantité est positive on modifie sinon on supprime l'article
      if ($qteProduit > 0)
      {
         //Recharche du produit dans le panier
         $positionProduit = array_search($libelleProduit,  $_SESSION['panier']['libelleProduit']);
 
         if ($positionProduit !== false)
         {
            $_SESSION['panier']['qteProduit'][$positionProduit] = $qteProduit ;
         }
      }
      else
      supprimerArticle($libelleProduit);
   }
   else
   echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
 
/**
 * Supprime un article du panier
 * @param $libelleProduit
 * @return unknown_type
 */
function supprimerArticle($libelleProduit){
   //Si le panier existe
   if (creationPanier() && !isVerrouille())
   {
      //Nous allons passer par un panier temporaire
      $tmp=array();
      $tmp['libelleProduit'] = array();
      $tmp['qteProduit'] = array();
      $tmp['prixProduit'] = array();
	  $tmp['tailleProduit'] = array();//champs temporaire pour la taille
      $tmp['verrou'] = $_SESSION['panier']['verrou'];
 
      for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++)
      {
         if ($_SESSION['panier']['libelleProduit'][$i] !== $libelleProduit)
         {
            array_push( $tmp['libelleProduit'],$_SESSION['panier']['libelleProduit'][$i]);
            array_push( $tmp['qteProduit'],$_SESSION['panier']['qteProduit'][$i]);
            array_push( $tmp['prixProduit'],$_SESSION['panier']['prixProduit'][$i]);
			array_push( $tmp['tailleProduit'],$_SESSION['panier']['tailleProduit'][$i]);//recuperation de la taille
         }
 
      }
      //On remplace le panier en session par notre panier temporaire à jour
      $_SESSION['panier'] =  $tmp;
      //On efface notre panier temporaire
      unset($tmp);
   }
   else
   echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
 
 
/**
 * Montant total du panier
 * @return int
 */
function MontantGlobal(){
   $total=0;
   for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++)
   {
      $total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
   }
   return $total;
}
 
 
/**
 * Fonction de suppression du panier
 * @return void
 */
function supprimePanier(){
   unset($_SESSION['panier']);
}
 
/**
 * Permet de savoir si le panier est verrouillé
 * @return booleen
 */
function isVerrouille(){
   if (isset($_SESSION['panier']) && $_SESSION['panier']['verrou'])
   return true;
   else
   return false;
}
 
/**
 * Compte le nombre d'articles différents dans le panier
 * @return int
 */
function compterArticles()
{
   if (isset($_SESSION['panier']))
   return count($_SESSION['panier']['libelleProduit']);
   else
   return 0;
 
}
 
?>
et pour la page panier :
panier.php :
Code :
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
<?php
session_start();
include_once("fonctions-panier.php");
 
$erreur = false;
 
$action = (isset($_POST['action'])? $_POST['action']:  (isset($_GET['action'])? $_GET['action']:null )) ;
if($action !== null)
{
   if(!in_array($action,array('ajout', 'suppression', 'refresh')))
   $erreur=true;
 
   //récuperation des variables en POST ou GET
   $l = (isset($_POST['l'])? $_POST['l']:  (isset($_GET['l'])? $_GET['l']:null )) ;
   $p = (isset($_POST['p'])? $_POST['p']:  (isset($_GET['p'])? $_GET['p']:null )) ;
   $q = (isset($_POST['q'])? $_POST['q']:  (isset($_GET['q'])? $_GET['q']:null )) ;
 
   //Suppression des espaces verticaux
   $l = preg_replace('#\v#', '',$l);
   //On verifie que $p soit un float
   $p = floatval($p);
 
   //On traite $q qui peut etre un entier simple ou un tableau d'entier
 
   if (is_array($q)){
      $QteArticle = array();
      $i=0;
      foreach ($q as $contenu){
         $QteArticle[$i++] = intval($contenu);
      }
   }
   else
   $q = intval($q);
 
}
 
if (!$erreur){
   switch($action){
      Case "ajout":
         ajouterArticle($l,$q,$p);
         break;
 
      Case "suppression":
         supprimerArticle($l);
         break;
 
      Case "refresh" :
         for ($i = 0 ; $i < count($QteArticle) ; $i++)
         {
            modifierQTeArticle($_SESSION['panier']['libelleProduit'][$i],round($QteArticle[$i]));
         }
         break;
 
      Default:
         break;
   }
}
 
echo '<?xml version="1.0" encoding="utf-8"?>';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>Votre panier</title>
</head>
<body>
 
<form method="post" action="panier.php">
<table style="width: 400px">
	<tr>
		<td colspan="4">Votre panier</td>
	</tr>
	<tr>
		<td>Libellé</td>
		<td>Quantité</td>
		<td>Taille</td><!--champs taille-->
		<td>Prix Unitaire</td>
		<td>Action</td>
	</tr>
 
 
	<?php
	if (creationPanier())
	{
	   $nbArticles=count($_SESSION['panier']['libelleProduit']);
	   if ($nbArticles <= 0)
	   echo "<tr><td>Votre panier est vide </ td></tr>";
	   else
	   {
	      for ($i=0 ;$i < $nbArticles ; $i++)
	      {
	         echo "<tr>";
	         echo "<td>".htmlspecialchars($_SESSION['panier']['libelleProduit'][$i])."</ td>";
	         echo "<td><input type=\"text\" size=\"4\" name=\"q[]\" value=\"".htmlspecialchars($_SESSION['panier']['qteProduit'][$i])."\"/></td>";
			 echo "<td>".htmlspecialchars($_SESSION['panier']['tailleProduit'][$i])."</td>";//champs pour la taille
	         echo "<td>".htmlspecialchars($_SESSION['panier']['prixProduit'][$i])."</td>";
	         echo "<td><a href=\"".htmlspecialchars("panier.php?action=suppression&l=".rawurlencode($_SESSION['panier']['libelleProduit'][$i]))."\">XX</a></td>";
	         echo "</tr>";
	      }
 
	      echo "<tr><td colspan=\"3\"> </td>";
	      echo "<td colspan=\"2\">";
	      echo "Total : ".MontantGlobal();
	      echo "</td></tr>";
 
	      echo "<tr><td colspan=\"5\">";
	      echo "<input type=\"submit\" value=\"Rafraichir\"/>";
	      echo "<input type=\"hidden\" name=\"action\" value=\"refresh\"/>";
 
	      echo "</td></tr>";
	   }
	}
	?>
</table>
</form>
</body>
</html>
__________________
Développeur informatique contrarié...
vorace est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 22h11.


 
 
 
 
Partenaires

Hébergement Web