Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1858 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  panier et session

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

panier et session

n°1674805
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 10:07:24  profilanswer
 

Bonjour,  
je suis en train de créer un panier en utilisant les sessions.
J'ai une première page catalogue ou sont listés les objets. Chaque objet à son propre bouton ajouter. Celui ci nous amène sur la page suivante que récapitule le panier. Mon problème est que sur cette nouvelle page, j'ai un retour possible au  catalogue. Voici le code :

Code :
  1. <a href="index.php<?php SID; ?>">Articles</a>


Mais lorsque je veux revalider un autre article, le panier est effacé et n'apparait que le dernier article choisi.
 
Comment résoudre mon problème?
 
Merci de m'aider.

mood
Publicité
Posté le 22-01-2008 à 10:07:24  profilanswer
 

n°1674806
MagicBuzz
Posté le 22-01-2008 à 10:11:38  profilanswer
 

http://img263.imageshack.us/img263/1195/img2439a0fp.jpg
 
C'est pas facile de lire du code à travers une boule de crytal, tu sais...

n°1674809
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 10:14:10  profilanswer
 

oups, désolée. :pt1cable:  
page catalogue (index.php) :

Code :
  1. <?php
  2. if ($_SESSION['panier'])
  3. session_start($_SESSION['panier']);
  4. elseif (session_start());
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10. <title>Catalogue</title>
  11. <link rel="stylesheet" href="fichier.css" type="text/css">
  12. </head>
  13. <body>
  14. <table width="100%">
  15. <tr>
  16.      <td class="Style1"><p></p><p>Ma boutique en ligne.</p></td>
  17.     </tr>
  18.     <tr>
  19.      <td class="Style2">Panier ( )</td>
  20. </table>
  21. <fieldset>
  22. <legend align="left">
  23.     <span id="cadre">Articles</span>
  24.     </legend>
  25. <table width="100%">
  26. <tr>
  27.      <td>
  28.         <table class="Style3">
  29.          <tr>
  30.              <td colspan="2" class="Style4">Canard de bain</td>
  31.             </tr>
  32.             <tr>
  33.              <td><img src="images/canard.gif" title="Canard de bain" height="100" width="100"></td>
  34.                 <td><p>Un compagnon de jeu pour le bain.</p>
  35.                  <p><b>2.99 Eur</b></p>
  36.                     <p><form method="POST" action="cart.php">
  37.                     <input type="hidden" name="code" value="ART001">
  38.                     <input type="hidden" name="article" value="canard">
  39.     <input type="hidden" name="qte" value="1">
  40.     <input type="hidden" name="prix" value="2.99">
  41.     <input type="hidden" name="tva" value="19.6">
  42.     <input type="hidden" name="poids" value="0.300">
  43.                     <input type="submit" name="ajouter" value="ajouter"></form></p>
  44.             </td>
  45.             </tr>
  46.         </table>
  47.         </td>
  48.         <td>
  49.         <table class="Style3">
  50.          <tr>
  51.              <td colspan="2" class="Style4">Compas</td>
  52.             </tr>
  53.             <tr>
  54.              <td><img src="images/compas.jpg" title="Compas" height="100" width="100"></td>
  55.                 <td><p>Pour ne pas perdre le nord.</p>
  56.                  <p><b>9.95 Eur</b></p>
  57.                     <p><form method="POST" action="cart.php">
  58.                     <input type="hidden" name="code" value="ART002">
  59.                     <input type="hidden" name="article" value="Compas">
  60.     <input type="hidden" name="qte" value="1">
  61.                     <input type="hidden" name="prix" value="9.95">
  62.     <input type="hidden" name="tva" value="19.6">
  63.     <input type="hidden" name="poids" value="0.900">
  64.     <input type="submit" name="ajouter" value="ajouter"></form></p>
  65.                 </td>
  66.             </tr>
  67.         </table>
  68.         </td>
  69.         <td>
  70.         <table class="Style3">
  71.          <tr>
  72.              <td colspan="2" class="Style4">Aquarium et poisson rouge</td>
  73.             </tr>
  74.             <tr>
  75.              <td><img src="images/aquarium.jpg" title="Aquarium" height="100" width="100"></td>
  76.                 <td><p>Pour passer le temps.</p>
  77.                  <p><b>24.95 Eur</b></p>
  78.                     <p><form method="POST" action="cart.php">
  79.                     <input type="hidden" name="code" value="ART003">
  80.                     <input type="hidden" name="article" value="Aquarium">
  81.     <input type="hidden" name="qte" value="1">
  82.                     <input type="hidden" name="prix" value="24.95">
  83.     <input type="hidden" name="tva" value="19.6">
  84.     <input type="hidden" name="poids" value="3.500">
  85.                     <input type="submit" name="aquarium" value="ajouter"></form></p>
  86.                 </td>
  87.             </tr>
  88.         </table>
  89.         </td>
  90.         <td>
  91.         <table class="Style3">
  92.          <tr>
  93.              <td colspan="2" class="Style4">Boite à lettres</td>
  94.             </tr>
  95.             <tr>
  96.              <td><img src="images/boite.gif" title="Boite à lettres" height="100" width="100"></td>
  97.                 <td><p>Vous avez du courrier.</p>
  98.                  <p><b>12.99 Eur</b></p>
  99.                     <p><form method="POST" action="cart.php">
  100.                     <input type="hidden" name="code" value="ART004">
  101.                     <input type="hidden" name="article" value="Boite à lettres">
  102.     <input type="hidden" name="qte" value="1">
  103.                     <input type="hidden" name="prix" value="12.99">
  104.     <input type="hidden" name="tva" value="19.6">
  105.     <input type="hidden" name="poids" value="5.000">
  106.                     <input type="submit" name="boite" value="ajouter"></form></p>
  107.                 </td>
  108.             </tr>
  109.         </table>
  110.         </td>
  111.     </tr>
  112. </table>
  113. </fieldset>
  114. <p>(c) 2008 - Ma Boutique en ligne - 1.0.0</p>
  115. </body>
  116. </html>


 
et page panier (cart.php) :

Code :
  1. <?
  2. session_start();
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <title>Panier</title>
  9. <link rel="stylesheet" href="fichier.css" type="text/css">
  10. </head>
  11. <body>
  12. <table width="100%">
  13. <tr>
  14.      <td class="Style1"><p></p><p>Ma boutique en ligne.</p></td>
  15.     </tr>
  16.     <tr>
  17.      <td class="Style2"><a href="index.php<?php SID; ?>">Articles</a> | <a href="invoice.php">Paiement</a></td>
  18. </table>
  19. <?php
  20. $_SESSION['article'] = $_POST['article'];
  21. $_SESSION['code'] = $_POST['code'];
  22. $_SESSION['qte'] = $_POST['qte'];
  23. $_SESSION['prix'] = $_POST['prix'];
  24. $_SESSION['tva'] = $_POST['tva'];
  25. $_SESSION['poids'] = $_POST['poids'];
  26. $total = $_SESSION['prix'] * $_SESSION['qte'];
  27.    
  28. $nbArticles=count($_SESSION['article']);
  29.   if ($nbArticles <= 0)
  30.    echo "<tr><td>Votre panier est vide </ td></tr>";
  31.   else
  32.   {
  33. ?>
  34. <fieldset>
  35. <legend align="left">
  36.     <span id="cadre">Panier</span>
  37.     </legend>
  38. <table width="100%">
  39. <tr>
  40.      <td class="Style11" width="16%">Code article</td>
  41.         <td class="Style11" width="16%">Produit</td>
  42.         <td class="Style11" width="16%">Quantité</td>
  43.         <td class="Style11" width="16%">Prix</td>
  44.         <td class="Style11" width="16%">Total</td>
  45.         <td class="Style11" width="16%"></td>
  46.     </tr>
  47. <?php
  48.    for ($i=0 ;$i < $nbArticles ; $i++)
  49.    {
  50. echo '<tr>';
  51. echo '<td class="Style3" width="16%">';
  52. echo $_SESSION['code'];
  53. echo '</td>';
  54. echo '<td class="Style31" width="16%">';
  55. echo $_SESSION['article'];
  56. echo '</td>';
  57. echo '<td class="Style3" width="16%">';
  58. echo '<img src="images/moins.gif">&nbsp;';
  59. echo $_SESSION['qte'];
  60. echo '&nbsp;<img src="images/plus.gif">';
  61. echo '</td>';
  62. echo '<td class="Style3" width="16%">';
  63. echo $_SESSION['prix'];
  64. echo '</td>';
  65. echo '<td class="Style3" width="16%">';
  66. echo $total;
  67. echo '</td>';
  68. echo '<td class="Style3" width="16%">';
  69. echo ' ';
  70. echo '</td>';
  71. echo '</tr>';
  72. }
  73.   }
  74. ?>
  75. </table>
  76. <table width="100%">
  77. <tr>
  78.      <td class="Style32" width="16%"></td>
  79.         <td class="Style32" width="16%"></td>
  80.         <td class="Style32" width="16%"></td>
  81.         <td class="Style32" width="16%">TOTAL</td>
  82.         <td class="Style32" width="16%">
  83.          <?php
  84.             $total2 = '12';
  85.   echo $total2;
  86.   ?>
  87.         </td>
  88.         <td class="Style32" width="16%"><a href="?vide=ok">Vider</a></td>
  89.     </tr>
  90. </table>   
  91. </fieldset>
  92. </body>
  93. </html>


Message édité par jereln le 22-01-2008 à 10:24:52
n°1674815
MagicBuzz
Posté le 22-01-2008 à 10:18:52  profilanswer
 

ligne 2 du premier fichier :
 
if ($_SESSION['pannier'])
 
=> Y'a une faute d'orthographe, et à mon avis, ça va avoir des effets de bords pas bien sympa, genre écrasement de la session systématiquement.
 
Et je ne connais pas PHP, mais les Session_Start un peu partout, ça me fait un peu peur, pour moi ça redémarre la session à chaque fois...

n°1674821
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 10:24:32  profilanswer
 

bien vu pour la faute d'orthographe!!
 
je ne sais pas comment faire pour garder ma session en revenant sur ma page catalogue...

n°1674827
MagicBuzz
Posté le 22-01-2008 à 10:27:03  profilanswer
 

à mon avis, tu dois virer tous tes session_start() de tout ton code entier.
 
et ajouter ce test dans toutes les pages :

Code :
  1. if (!$_SESSION['panier'])
  2.  session_start($_SESSION['panier']);


 
Et rien d'autre.
Je pense.
Après je ne connais pas assez PHP pour jurer que ça marche.

n°1674829
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 10:28:25  profilanswer
 

ok, j'essaye et te tiens au courant. merci
 
 
code testé et toujours pareil : juste le dernier article ajouter. pffff!!!


Message édité par jereln le 22-01-2008 à 10:30:22
n°1674830
MagicBuzz
Posté le 22-01-2008 à 10:28:36  profilanswer
 

ah, et...
 
y'a une couille dans le potage quand même...
c'est quoi tes sessions "prix", "qte", et autres ?
 
c'est pas censés être des membres de ta session panier ?
 
logiquement, un panier, c'est un array de structs, et tu ajoutes un élément à chaque fois que t'ajoute un produit non... ?

n°1674836
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 10:33:11  profilanswer
 

en fait je veux ajouter toutes les données correspondant à l'article dans le panier. je ne sais pas trop comment faire pour touit enregister dans ma session...

n°1674839
MagicBuzz
Posté le 22-01-2008 à 10:34:28  profilanswer
 

En fait, un panier ça ressemble plutôt à ça :
 

Code :
  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Data.OracleClient;
  5.  
  6. namespace bci
  7. {
  8.     /// <summary>
  9.     /// Summary description for orderObjects.
  10.     /// </summary>
  11.     public class tie
  12.     {
  13.         public decimal codsoc;
  14.         public string typtie;
  15.         public string sigtie;
  16.         public string nomtie;
  17.         public string franco;
  18.  
  19.         private ArrayList addresses = new ArrayList();
  20.  
  21.         public tie(string cnxString, decimal codsoc, string typtie, string sigtie)
  22.         {
  23.             Queries commander = new Queries(cnxString);
  24.             
  25.             DataTable dtTie = commander.LoadTie(codsoc, typtie, sigtie);
  26.             if (dtTie != null && dtTie.Rows.Count > 0)
  27.             {
  28.                 this.codsoc = (decimal)dtTie.Rows[0]["codsoc"];
  29.                 this.typtie = (string)dtTie.Rows[0]["typtie"];
  30.                 this.sigtie = (string)dtTie.Rows[0]["sigtie"];
  31.                 this.nomtie = (string)dtTie.Rows[0]["nomtie"];
  32.                 this.franco = string.Empty;
  33.  
  34.                 DataTable dtAdrs = commander.LoadTieAdr(codsoc, typtie, sigtie);
  35.                 if (dtAdrs != null)
  36.                 {
  37.                     foreach (DataRow dr in dtAdrs.Rows)
  38.                     {
  39.                         this.addAddress(new adr(cnxString, (decimal)dr["codsoc"], (string)dr["typtie"], (string)dr["sigtie"], (string)dr["typadr"], (decimal)dr["numadr"]));
  40.                     }
  41.                 }
  42.             }
  43.             dtTie.Dispose();
  44.             dtTie = null;
  45.             commander.Close();
  46.             commander = null;
  47.         }
  48.  
  49.         public void addAddress(adr address)
  50.         {
  51.             this.addresses.Add(address);
  52.         }
  53.  
  54.         public ArrayList getAddress(string addrType)
  55.         {
  56.             ArrayList ret = new ArrayList();
  57.             foreach (adr address in this.addresses)
  58.             {
  59.                 if (address.typadr == addrType)
  60.                     ret.Add(address);
  61.             }
  62.             return ret;
  63.         }
  64.  
  65.         public adr getAddress(string addrType, decimal numadr)
  66.         {
  67.             foreach (adr address in this.addresses)
  68.             {
  69.                 if (address.typadr == addrType && address.numadr == numadr)
  70.                     return address;
  71.             }
  72.             return null;
  73.         }
  74.     }
  75.  
  76.     public class adr
  77.     {
  78.         public decimal codsoc;
  79.         public string typtie;
  80.         public string typadr;
  81.         public string sigadr;
  82.         public decimal numadr;
  83.         public string libadr;
  84.         public string adress;
  85.         public string adrsui;
  86.         public string locali;
  87.         public string codpos;
  88.         public string cenpos;
  89.         public string pays;
  90.         public string codciv;
  91.         public string prenom;
  92.         public string contac;
  93.         public string tel;
  94.         public string fax;
  95.         public string adredi;
  96.  
  97.         public adr(string cnxString, decimal codsoc, string typtie, string sigadr, string typadr, decimal numadr)
  98.         {
  99.             Queries commander = new Queries(cnxString);
  100.             
  101.             DataTable dtAdr = commander.LoadAdr(codsoc, typtie, sigadr, typadr, numadr);
  102.             if (dtAdr != null && dtAdr.Rows.Count > 0)
  103.             {
  104.                 this.codsoc = (decimal)dtAdr.Rows[0]["codsoc"];
  105.                 this.typtie = (string)dtAdr.Rows[0]["typtie"];
  106.                 this.sigadr = (string)dtAdr.Rows[0]["sigtie"];
  107.                 this.typadr = (string)dtAdr.Rows[0]["typadr"];
  108.                 this.numadr = (decimal)dtAdr.Rows[0]["numadr"];
  109.                 this.libadr = (string)dtAdr.Rows[0]["libadr"];
  110.                 this.adress = (string)dtAdr.Rows[0]["adress"];
  111.                 this.adrsui = (string)dtAdr.Rows[0]["adrsui"];
  112.                 this.locali = (string)dtAdr.Rows[0]["locali"];
  113.                 this.codpos = (string)dtAdr.Rows[0]["codpos"];
  114.                 this.cenpos = (string)dtAdr.Rows[0]["cenpos"];
  115.                 this.pays = (string)dtAdr.Rows[0]["pays"];
  116.                 this.codciv = (string)dtAdr.Rows[0]["codciv"];
  117.                 this.prenom = (string)dtAdr.Rows[0]["prenom"];
  118.                 this.contac = (string)dtAdr.Rows[0]["contac"];
  119.                 this.tel = (string)dtAdr.Rows[0]["tel"];
  120.                 this.fax = (string)dtAdr.Rows[0]["fax"];
  121.                 this.adredi = (string)dtAdr.Rows[0]["adredi"];
  122.             }
  123.             dtAdr.Dispose();
  124.             dtAdr = null;
  125.             commander.Close();
  126.             commander = null;
  127.         }
  128.     }
  129.  
  130.     public class pro
  131.     {
  132.         public decimal codsoc;
  133.         public string codpro;
  134.         public string refpro;
  135.         public string fampro;
  136.         public string sfapro;
  137.         public string ssfpro;
  138.         public string codblocage;
  139.         public string nompro;
  140.         public string tradesig1;
  141.         public string tradesig2;
  142.         public string tradesig3;
  143.         public decimal prxtar;
  144.         public string coddev;
  145.         public int qotite;
  146.         public int large;
  147.         public int haute;
  148.         public int longue;
  149.         public tie fou;
  150.  
  151.         public pro(string cnxString, decimal codsoc, string codpro, string codlan)
  152.         {
  153.             Queries commander = new Queries(cnxString);
  154.             
  155.             DataTable dtPro = commander.LoadPro(codsoc, codpro, codlan);
  156.             if (dtPro != null && dtPro.Rows.Count > 0)
  157.             {
  158.                 this.codsoc = (decimal)dtPro.Rows[0]["codsoc"];
  159.                 this.codpro = (string)dtPro.Rows[0]["codpro"];
  160.                 this.refpro = (string)dtPro.Rows[0]["refpro"];
  161.                 this.fampro = (string)dtPro.Rows[0]["fampro"];
  162.                 this.sfapro = (string)dtPro.Rows[0]["sfapro"];
  163.                 this.ssfpro = (string)dtPro.Rows[0]["ssfpro"];
  164.                 this.codblocage = (string)dtPro.Rows[0]["codblocage"];
  165.                 this.nompro = (string)dtPro.Rows[0]["nompro"];
  166.                 this.tradesig1 = (string)dtPro.Rows[0]["tradesig1"];
  167.                 this.tradesig2 = (string)dtPro.Rows[0]["tradesig2"];
  168.                 this.tradesig3 = (string)dtPro.Rows[0]["tradesig3"];
  169.                 this.prxtar = (decimal)dtPro.Rows[0]["prxtar"];
  170.                 this.coddev = (string)dtPro.Rows[0]["coddev"];
  171.  
  172.                 this.fou = new tie(cnxString, this.codsoc, "FOU", (string)dtPro.Rows[0]["sigfou"]);
  173.             }
  174.             dtPro.Dispose();
  175.             dtPro = null;
  176.             commander.Close();
  177.             commander = null;
  178.         }
  179.     }
  180.  
  181.     public class evp
  182.     {
  183.         public pro product;
  184.         public long qtecde;
  185.  
  186.         public evp(string cnxString, decimal codsoc, string codpro, string codlan, long qtecde)
  187.         {
  188.             this.product = new pro(cnxString, codsoc, codpro, codlan);
  189.             this.qtecde = qtecde;
  190.         }
  191.  
  192.         public decimal prxvdu
  193.         {
  194.             get
  195.             {
  196.                 return (qtecde * product.prxtar);
  197.             }
  198.         }
  199.     }
  200.  
  201.     public class eve
  202.     {
  203.         private ArrayList postes = new ArrayList();
  204.         public tie client;
  205.  
  206.         public eve()
  207.         {
  208.         }
  209.  
  210.         public eve(string cnxString, decimal codsoc, string typtie, string sigtie)
  211.         {
  212.             this.client = new tie(cnxString, codsoc, typtie, sigtie);
  213.         }
  214.  
  215.         public string[] getListFou()
  216.         {
  217.             ArrayList listFou = new ArrayList();
  218.  
  219.             for (int i = 0, nb = this.postCount; i < nb; i++)
  220.             {
  221.                 if (listFou.IndexOf(((evp)this.postes[i]).product.fou.sigtie) == -1)
  222.                 {
  223.                     listFou.Add(((evp)this.postes[i]).product.fou.sigtie);
  224.                 }
  225.             }
  226.             return (string[])listFou.ToArray(typeof(string));
  227.         }
  228.  
  229.         public int postCount
  230.         {
  231.             get
  232.             {
  233.                 return postes.Count;
  234.             }
  235.         }
  236.  
  237.         public decimal prxvdu
  238.         {
  239.             get
  240.             {
  241.                 decimal tmp = 0;
  242.                 foreach (evp post in this.postes)
  243.                 {
  244.                     tmp += post.prxvdu;
  245.                 }
  246.                 return tmp;
  247.             }
  248.         }
  249.  
  250.         public long qtecde
  251.         {
  252.             get
  253.             {
  254.                 long tmp = 0;
  255.                 foreach (evp post in this.postes)
  256.                 {
  257.                     tmp += post.qtecde;
  258.                 }
  259.                 return tmp;
  260.             }
  261.         }
  262.  
  263.         public string coddev
  264.         {
  265.             get
  266.             {
  267.                 if (this.postCount != 0)
  268.                 {
  269.                     return ToolBox.CoddevToSymbol(((evp)this.postes[0]).product.coddev);
  270.                 }
  271.                 else
  272.                 {
  273.                     return string.Empty;
  274.                 }
  275.             }
  276.         }
  277.  
  278.         public void addPro(string cnxString, decimal codsoc, string codpro, string codlan)
  279.         {
  280.             evp poste = this.getPost(codpro);
  281.             if (poste != null)
  282.             {
  283.                 poste.qtecde++;
  284.             }
  285.             else
  286.             {
  287.                 this.postes.Add(new evp(cnxString, codsoc, codpro, codlan, 1));
  288.             }
  289.         }
  290.  
  291.         public void addPost(evp post)
  292.         {
  293.             bool found = false;
  294.  
  295.             foreach (evp p in postes)
  296.             {
  297.                 if (p.product.codpro == post.product.codpro)
  298.                 {
  299.                     p.qtecde += post.qtecde;
  300.                     found = true;
  301.                     break;
  302.                 }
  303.             }
  304.             
  305.             if (!found)
  306.             {
  307.                 postes.Add(post);
  308.             }
  309.         }
  310.  
  311.         public void delPro(string codpro)
  312.         {
  313.             evp poste = this.getPost(codpro);
  314.             if (poste != null)
  315.             {
  316.                 if (poste.qtecde > 1)
  317.                 {
  318.                     poste.qtecde--;
  319.                 }
  320.                 else
  321.                 {
  322.                     this.postes.Remove(poste);
  323.                 }
  324.             }
  325.         }
  326.  
  327.         public void chgPro(string cnxString, decimal codsoc, string codpro, string codlan, long qtecde)
  328.         {
  329.             evp poste = this.getPost(codpro);
  330.             if (poste != null)
  331.             {
  332.                 if (qtecde == 0)
  333.                 {
  334.                     this.postes.Remove(poste);
  335.                 }
  336.                 else
  337.                 {
  338.                     poste.qtecde = qtecde;
  339.                 }
  340.             }
  341.             else
  342.             {
  343.                 this.postes.Add(new evp(cnxString, codsoc, codpro, codlan, qtecde));
  344.             }
  345.         }
  346.  
  347.         public evp getPost(int index)
  348.         {
  349.             return ((evp) this.postes[index]);
  350.         }
  351.  
  352.         public evp[] getPostes(string sigfou)
  353.         {
  354.             ArrayList postes = new ArrayList();
  355.  
  356.             for (int i = 0, nb = this.postCount; i < nb; i++)
  357.             {
  358.                 if (((evp)this.postes[i]).product.fou.sigtie == sigfou)
  359.                 {
  360.                     postes.Add(this.postes[i]);
  361.                 }
  362.             }
  363.  
  364.             return (evp[])postes.ToArray(typeof(evp));
  365.         }
  366.  
  367.         public evp getPost(pro product)
  368.         {
  369.             foreach (evp post in this.postes)
  370.             {
  371.                 if (post.product.codpro == product.codpro)
  372.                     return post;
  373.             }
  374.  
  375.             return null;
  376.         }
  377.  
  378.         public evp getPost(string codpro)
  379.         {
  380.             foreach (evp post in this.postes)
  381.             {
  382.                 if (post.product.codpro == codpro)
  383.                     return post;
  384.             }
  385.  
  386.             return null;
  387.         }
  388.  
  389.         public bool updateQtecde(int index, int change)
  390.         {
  391.             ((evp) this.postes[index]).qtecde += change;
  392.             return true;
  393.         }
  394.  
  395.         public bool updateQtecde(pro product, int change)
  396.         {
  397.             foreach (evp post in this.postes)
  398.             {
  399.                 if (post.product.codpro == product.codpro)
  400.                 {
  401.                     post.qtecde += change;
  402.                     return true;
  403.                 }
  404.             }
  405.             return false;
  406.         }
  407.  
  408.         public void removePost(int index)
  409.         {
  410.             this.postes.RemoveAt(index);
  411.         }
  412.  
  413.         public void removePost(pro product)
  414.         {
  415.             foreach (evp post in this.postes)
  416.             {
  417.                 if (post.product.codpro == product.codpro)
  418.                 {
  419.                     this.postes.Remove(post);
  420.                     return;
  421.                 }
  422.             }
  423.         }
  424.     }
  425. }


 
Avec :
 
- EVE : Point d'entrée du panier
- EVP : Ligne du panier
- PRO : Produit
- TIE : Client
- ADR : Adresse
 
Bon, là c'est du C# et c'est spécifique à un site que j'ai écrit pour un client, mais disons que ça donne une base de modèle...
 
 
Ensuite, ça c'est le test effectué partout là où j'ai besoin de mon panier :

Code :
  1. if (Session["eve"] == null)
  2.             {
  3.                 Session["eve"] = new eve(string.Format((string) Application["cnxString"], (string) Application["dbLogin"], (string) Application["dbPass"], (string) Application["dbTNS"]), (decimal)Session["codsoc"], (string)Session["typtie"], ((string)Session["login"]).ToUpper());
  4.             }

mood
Publicité
Posté le 22-01-2008 à 10:34:28  profilanswer
 

n°1674852
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 10:43:34  profilanswer
 

je ne connais pas du tout le C#, je vais essayer de comprendre.
 

n°1674925
leflos5
On est ou on est pas :)
Posté le 22-01-2008 à 11:38:35  profilanswer
 

C'est surtout que faut sécuriser ça parce que sinon ça risque d'être assez surprenant :whistle:
 
En effet y'a une belle couille dans les session_start(), doit y en avoir un et un seul, en début de script, avant toute sortie vers le navigateur.
Y'a pas de paramètre à passer, il récupère celle qu'il trouve via l'identifiant de session récupéré par GET, POST ou COOKIE ;)
 
Si tu veux faire des vérifs et regénérer un identifiant ou tuer la session t'as session_regenerate_id() et session_destroy()

n°1674986
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 13:01:33  profilanswer
 

ok pour les session_start, merci.
 
mais ce que je veux, c'est conservé mon panier lorsque je reviens sur ma page cataloge pour prendre un autre article. et c'est là que ça bloque...

n°1674996
sircam
I Like Trains
Posté le 22-01-2008 à 13:30:35  profilanswer
 

Commence peut-être par réduire la portée du problème en créant deux ou trois pages réduites à leur plus simple expression pour tester ton panier, en vérifiant à chaque fois ce qui est dedans et ce qui ne l'est pas.


---------------
Now Playing: {SYNTAX ERROR AT LINE 1210}
n°1675104
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 15:10:06  profilanswer
 

je tourne en rond et ne vois plus mon code, quelqu'un peut-il m'aider?

n°1675123
skeye
Posté le 22-01-2008 à 15:25:40  profilanswer
 

jereln a écrit :

je tourne en rond et ne vois plus mon code, quelqu'un peut-il m'aider?


Je ne vois pas comment.
Tu ne comprends manifestement pas comment sont censées fonctionner les sessions.
Commence par lire la doc du site php.net sur les sessions, ainsi que les exemples, quitte à te faire quelques petits scripts de test pour bien assimiler comment ça fonctionne.
Ensuite tu pourras adapter à ton problème.
 
Là on dirait que tu te lances au hasard là-dedans sans savoir à l'avance ce qui va se passer...


---------------
Can't buy what I want because it's free -
n°1675124
omega2
Posté le 22-01-2008 à 15:28:13  profilanswer
 

jereln > En php, la structure classique de stockage d'un panier c'est :

Citation :


array (
  array ( element, quantite),
  array ( element, quantite),
  array ( element, quantite)
  )
)

A noter que si tu veux les stocker dans la session alors c'est le tableau principal (le array extérieur) qu'il faut stocker directement dans la session et pas chaque élément séparé.
 
De ton côté, dans ton code, tu stockes chaque caractéristique du produit directement dans une case de la session. Ensuite quand tu repasses pour mettre un autre produit, tu écrases ce que t'avais avant et tu mets les caractéristiques du nouveau produit à la place. C'est normal que tu n'ai pas le résultat voulut.

n°1675127
dwogsi
Défaillance cérébrale...
Posté le 22-01-2008 à 15:33:30  profilanswer
 

Bon j'ai lu le topic en diagonal... :o
Mais de ce que j'ai vu dans ton code, je dirais qu'il n'est pas très surprenant que tu perdre ton panier puisqu'a chaque fois que tu ajoute quelque chose tu écrase ce qu'il y avait déjà... Utilise des tableaux!
 
Logiquement quant tu ajoute un produit dans ton panier ça devrait se passer ainsi :
Vérifier si le produit est déjà dans le panier, auquel cas il faut augmenter la quantité sinon ajouter simplement le produit.
 
Edit :  [:benou_grilled]


Message édité par dwogsi le 22-01-2008 à 15:34:26

---------------
-- Debian -- Le système d'exploitation universel | Le gras c'est la vie! | /(bb|[^b]{2})/
n°1675152
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 15:56:37  profilanswer
 

ok dwogsi, mais comment faire pour vérifier si l'article existe déjà?

n°1675159
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 16:06:18  profilanswer
 

bon, je reprends au début.
je garde ma page index.php
et sur ma page cart.php, je mets :

Code :
  1. <?php
  2. session_start();
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <title>Document sans titre</title>
  9. </head>
  10. <body>
  11. <?php
  12. echo("<pre>" ) ;
  13. print_r($_SESSION) ;
  14. echo("</pre>" ) ;
  15. ?></body>
  16. </html>


 
J'obtiens ça :
Array
(
    [panier] =>  
)
 
donc j'ai un soucis dès ma page index, non? si c'est ça, je ne vois pas.

n°1675164
skeye
Posté le 22-01-2008 à 16:08:23  profilanswer
 

Si ta page index.php est celle postée plus haut sans modifications, c'est n'importe-quoi donc il ne faut pas t'attendre à avoir quoi que ce soit de normal là. Si ce n'est pas celle postée plus haut, montre-la nous.


Message édité par skeye le 22-01-2008 à 16:08:45

---------------
Can't buy what I want because it's free -
n°1675167
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 16:13:32  profilanswer
 

je ne sais plus si j'ai fait des changement ou non, donc je te remets ma page index.php

Code :
  1. <?php
  2. session_start();
  3. $_SESSION['panier']=$panier;
  4. ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. <title>Catalogue</title>
  10. <link rel="stylesheet" href="fichier.css" type="text/css">
  11. </head>
  12. <body>
  13. <table width="100%">
  14. <tr>
  15.      <td class="Style1"><p></p><p>Ma boutique en ligne.</p></td>
  16.     </tr>
  17.     <tr>
  18.      <td class="Style2">Panier ( )</td>
  19. </table>
  20. <fieldset>
  21. <legend align="left">
  22.     <span id="cadre">Articles</span>
  23.     </legend>
  24. <table width="100%">
  25. <tr>
  26.      <td>
  27.         <table class="Style3">
  28.          <tr>
  29.              <td colspan="2" class="Style4">Canard de bain</td>
  30.             </tr>
  31.             <tr>
  32.              <td><img src="images/canard.gif" title="Canard de bain" height="100" width="100"></td>
  33.                 <td><p>Un compagnon de jeu pour le bain.</p>
  34.                  <p><b>2.99 Eur</b></p>
  35.                     <p><form method="POST" action="cart.php">
  36.                     <input type="hidden" name="code" value="ART001">
  37.                     <input type="hidden" name="article" value="canard">
  38.     <input type="hidden" name="qte" value="1">
  39.     <input type="hidden" name="prix" value="2.99">
  40.     <input type="hidden" name="tva" value="19.6">
  41.     <input type="hidden" name="poids" value="0.300">
  42.                     <input type="submit" name="ajouter" value="ajouter"></form></p>
  43.             </td>
  44.             </tr>
  45.         </table>
  46.         </td>
  47.         <td>
  48.         <table class="Style3">
  49.          <tr>
  50.              <td colspan="2" class="Style4">Compas</td>
  51.             </tr>
  52.             <tr>
  53.              <td><img src="images/compas.jpg" title="Compas" height="100" width="100"></td>
  54.                 <td><p>Pour ne pas perdre le nord.</p>
  55.                  <p><b>9.95 Eur</b></p>
  56.                     <p><form method="POST" action="cart.php">
  57.                     <input type="hidden" name="code" value="ART002">
  58.                     <input type="hidden" name="article" value="Compas">
  59.     <input type="hidden" name="qte" value="1">
  60.                     <input type="hidden" name="prix" value="9.95">
  61.     <input type="hidden" name="tva" value="19.6">
  62.     <input type="hidden" name="poids" value="0.900">
  63.     <input type="submit" name="ajouter" value="ajouter"></form></p>
  64.                 </td>
  65.             </tr>
  66.         </table>
  67.         </td>
  68.         <td>
  69.         <table class="Style3">
  70.          <tr>
  71.              <td colspan="2" class="Style4">Aquarium et poisson rouge</td>
  72.             </tr>
  73.             <tr>
  74.              <td><img src="images/aquarium.jpg" title="Aquarium" height="100" width="100"></td>
  75.                 <td><p>Pour passer le temps.</p>
  76.                  <p><b>24.95 Eur</b></p>
  77.                     <p><form method="POST" action="cart.php">
  78.                     <input type="hidden" name="code" value="ART003">
  79.                     <input type="hidden" name="article" value="Aquarium">
  80.     <input type="hidden" name="qte" value="1">
  81.                     <input type="hidden" name="prix" value="24.95">
  82.     <input type="hidden" name="tva" value="19.6">
  83.     <input type="hidden" name="poids" value="3.500">
  84.                     <input type="submit" name="aquarium" value="ajouter"></form></p>
  85.                 </td>
  86.             </tr>
  87.         </table>
  88.         </td>
  89.         <td>
  90.         <table class="Style3">
  91.          <tr>
  92.              <td colspan="2" class="Style4">Boite à lettres</td>
  93.             </tr>
  94.             <tr>
  95.              <td><img src="images/boite.gif" title="Boite à lettres" height="100" width="100"></td>
  96.                 <td><p>Vous avez du courrier.</p>
  97.                  <p><b>12.99 Eur</b></p>
  98.                     <p><form method="POST" action="cart.php">
  99.                     <input type="hidden" name="code" value="ART004">
  100.                     <input type="hidden" name="article" value="Boite à lettres">
  101.     <input type="hidden" name="qte" value="1">
  102.                     <input type="hidden" name="prix" value="12.99">
  103.     <input type="hidden" name="tva" value="19.6">
  104.     <input type="hidden" name="poids" value="5.000">
  105.                     <input type="submit" name="boite" value="ajouter"></form></p>
  106.                 </td>
  107.             </tr>
  108.         </table>
  109.         </td>
  110.     </tr>
  111. </table>
  112. </fieldset>
  113. <p>(c) 2008 - Ma Boutique en ligne - 1.0.0</p>
  114. </body>
  115. </html>


 
 
et ma page cart.php :

Code :
  1. <?php
  2. session_start();
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <title>Document sans titre</title>
  9. </head>
  10. <body>
  11. <?php
  12. $_SESSION["code"] = $_POST["code"];
  13. $_SESSION["article"] = $_POST["article"];
  14. echo("<pre>" ) ;
  15. print_r($_SESSION) ;
  16. echo("</pre>" ) ;
  17. ?></body>
  18. </html>


 
cette fois-ci, j'obtiens :
Array
(
    [panier] =>  
    [code] => ART001
    [article] => canard
)
 
le problème, c'est que ma session panier est écrasée à chaque fois, et du coup, je ne peux pas ajouter de nouveaux articles.

n°1675170
skeye
Posté le 22-01-2008 à 16:17:26  profilanswer
 

:sweat:
 
Je veux pas te donner l'impression de répéter systématiquement la même chose, mais lis la doc et les exemples sur les sessions...tu t'en sers comme si c'était magique...[:moule_bite]


---------------
Can't buy what I want because it's free -
n°1675171
omega2
Posté le 22-01-2008 à 16:18:57  profilanswer
 

Je me quote moi même vu ton dernier message.

omega2 a écrit :

jereln > En php, la structure classique de stockage d'un panier c'est :

Citation :


array (
  array ( element, quantite),
  array ( element, quantite),
  array ( element, quantite)
  )
)

A noter que si tu veux les stocker dans la session alors c'est le tableau principal (le array extérieur) qu'il faut stocker directement dans la session et pas chaque élément séparé.

En plus clair $_SESSION['machin'] (machin à remplacer par ce que tu veux) doit contenir un tableau de tableau.

n°1675172
jereln
..elle est pas belle la vie...
Posté le 22-01-2008 à 16:21:19  profilanswer
 

NazzTazz > donc au début de ma page index.php (c'est ça?), je dois mettre :

Code :
  1. session_start();
  2. if($panier==null){
  3. $_SESSION['panier']=$panier;
  4. }
  5. elseif{
  6. reprendre le panier existant
  7. }


 
si c'est ça comment je lui dit de continuer sur le panier existant?

n°1675173
omega2
Posté le 22-01-2008 à 16:24:47  profilanswer
 

jereln a écrit :

NazzTazz > donc au début de ma page index.php (c'est ça?), je dois mettre :

Code :
  1. session_start();
  2. if($panier==null){
  3. $_SESSION['panier']=$panier;
  4. }
  5. elseif{
  6. reprendre le panier existant
  7. }


 
si c'est ça comment je lui dit de continuer sur le panier existant?


Traduction de ton code : si le panier n'existe pas en dehors de la session (ce qui est toujours le cas, soit dit en passant, sauf réglage antisécuritaire du serveur ), je détruits ce que j'avais mis dans la session.
 
T'es sur de comprendre comment ça marche?


Message édité par omega2 le 22-01-2008 à 16:26:39
n°1675174
skeye
Posté le 22-01-2008 à 16:24:55  profilanswer
 

jereln a écrit :

NazzTazz > donc au début de ma page index.php (c'est ça?), je dois mettre :

Code :
  1. session_start();
  2. if($panier==null){
  3. $_SESSION['panier']=$panier;
  4. }




 
Mais comment tu veux qu'il y ait quelque chose dans ta variable $panier si tu n'y as rien mis avant?[:autobot]


---------------
Can't buy what I want because it's free -
n°1675176
skeye
Posté le 22-01-2008 à 16:26:32  profilanswer
 

Bon, je passe mon tour, moi. Il te manque des bases en php, et tu sembles refuser d'apprendre par toi-même en lisant la doc et les exemples.


---------------
Can't buy what I want because it's free -
n°1675183
dwogsi
Défaillance cérébrale...
Posté le 22-01-2008 à 16:34:19  profilanswer
 

J'ai hâte de voir la partie paiement!  :pt1cable:


Message édité par dwogsi le 22-01-2008 à 16:34:50

---------------
-- Debian -- Le système d'exploitation universel | Le gras c'est la vie! | /(bb|[^b]{2})/
mood
Publicité
Posté le   profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  panier et session

 

Sujets relatifs
gestion de la session Hibernate dans un WebService Axis 1.2powershell - Créer une session sous vista
[Résolu] Includes ne fonctionnent plus depuis session[PHP] Avis sur formulaire et session
[php/mysql] Ajout d'un prix variable dans le panierCheckbox et session
Session + javascriptliste menu associé a un panier
Bug avec le panier d'un site e-commerce[Java, JSP, Servlet] Problème de session
Plus de sujets relatifs à : panier et session


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)