IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Création panier avec le tutoriel de ce site


Sujet :

Langage PHP

  1. #1
    telodo
    Invité(e)
    Par défaut Création panier avec le tutoriel de ce site
    Bonsoir,


    J'utilise Wamp Server 2.0 et PHP 5.2.8 en local.

    J'ai suivis le tutoriel pour la création d'un panier en php, cependant pas moyen de faire fonctionner ce panier ! J'ai strictement fait un copier/coller du script pour etre sur de ne pas avoir fait de fautes de syntaxes.

    Je rappel le lien de ce super tutoriel : http://jcrozier.developpez.com/articles/web/panier/


    Voici les erreures :



    PS : J'ai oublie de refaire l'URL de l'ajout... Mais j'ai toujours les erreures avec href="panier.php?action=ajout&l=jean&q=10&p=30"


    Si vous pourriez m'aider à les déchiffrer et à solutionner ce problème cela m'aiderait grandement et me permettrait d'avancer dans la programmation PHP ! Dans l'attente d'une reponse, je vais tout de même tenter de trouver une solution, ou du moins le problème.

    Je vous remercie d'avance, vous souhaite une bonne soirée et de bonnes fêtes de fin d'année !

  2. #2
    Modérateur
    Avatar de blueice
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2003
    Messages
    3 487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2003
    Messages : 3 487
    Points : 5 134
    Points
    5 134
    Par défaut
    Sûrement un problème de code en copiant, tu devrais chercher à comprendre ce que tu tapes au lieu de bêtement recopier, ca éviterai ce genre de problème, voici le code de chaque page et en prime un dossier complet zippé qui fonctionne, je viens de tester.

    fonctions-panier.php :
    Code php : 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
    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
     
    <?php
    function creationPanier(){
    $ret=false;
     
    if (isset( $_SESSION['panier']))
     $ret = true;
    else
    {
     
      $_SESSION['panier']=array();
      $_SESSION['panier']['libelleProduit'] = array();
      $_SESSION['panier']['qteProduit'] = array();      
      $_SESSION['panier']['prixProduit'] = array();
      $ret=true;
    }
    return $ret;
    }
     
    function ajouterArticle($libelleProduit,$qteProduit,$prixProduit){
     
    if (creationPanier())
    {
    $positionProduit = array_search($libelleProduit,  $_SESSION['panier']['libelleProduit']);
     
      if ($positionProduit !== false)
      {
       $_SESSION['panier']['qteProduit'][$positionProduit] += $qteProduit ;
      }
      else
      {
       array_push( $_SESSION['panier']['libelleProduit'],$libelleProduit);
       array_push( $_SESSION['panier']['qteProduit'],$qteProduit); 
       array_push( $_SESSION['panier']['prixProduit'],$prixProduit);
      }
     
    }
     
    else
      echo "Un problème est survenu veuillez contacter l'administrateur du site.";
    }
     
    function supprimerArticle($libelleProduit){
     
    if (creationPanier())
    {
      $tmp=array();
      $tmp['libelleProduit'] = array();
      $tmp['qteProduit'] = array();      
      $tmp['prixProduit'] = array();
     
      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]);
       }
     
      }
     
     
    $_SESSION['panier'] =  $tmp;
    unset($tmp);      
     
    }
    else
      echo "Un problème est survenu veuillez contacter l'administrateur du site.";
    }
     
    function modifierQTeArticle($libelleProduit,$qteProduit){
    if (creationPanier())
    {
     
      if ($qteProduit > 0)
      {
       $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.";
    }
     
    function MontantGlobal(){
     
    $total=0;
     
      for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++) 
      {            
       $total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i]; 
      }
     
    return $total;
    }
    ?>
    panier.php :
    Code php : 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
    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
     
    <?php
     
    session_start();
    include_once("fonctions-panier.php");
     
    if (isset($_GET['action']))
    {
     $erreur=false;
     
     
     if(in_array( $_GET['action'],array('ajout', 'suppression', 'refresh')))
      $action = $_GET['action'];
     else
      $erreur=true;
     
     $l = preg_replace('#\v#', '', $_GET['l']);
     
     $q = intval($_GET['q']);
     
     $p = floatval($_GET['p']);
     
    }
     
    elseif(isset($_POST['action']))
    {
     unset($_GET);
     $erreur=false;
     
     if(in_array($_POST['action'],array('ajout', 'suppression', 'refresh')))
      $action=$_POST['action'];
     else
      $erreur=true;
     
      $l = preg_replace('#\v#', '',$_POST['l']);
     
      $p = floatval($_POST['p']);
     
     
     $QteArticle = array();
     
     $i=0;
     foreach ($_POST['QteArticle'] as $contenu){
      $QteArticle[$i++] = intval($contenu);
     
     }
     
    }
     
     
     
     
    if ($erreur==false){
     
     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;
     
     }
    }
     
     
     
    if ($erreur==false){
     
     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="iso-8859-1"?>';?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <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>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=\"QteArticle[]\" value=\"".htmlspecialchars($_SESSION['panier']['qteProduit'][$i])."\"/></td>";
        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=\"2\"> </td>";
      echo "<td colspan=\"2\">";
      echo "Total : ".MontantGlobal();
      echo "</td></tr>";
     
      echo "<tr><td colspan=\"4\">";
      echo "<input type=\"submit\" value=\"Rafraichir\"/>";
      echo "<input type=\"hidden\" name=\"action\" value=\"refresh\"/>";
     
      echo "</td></tr>";
      }
    }
    ?>
    </table>
    </form>
    </body>
    </html>

    index.php :
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    	<title>Untitled</title>
    </head>
    <body>
    <a href="panier.php?action=ajout&amp;l=LIBELLEPRODUIT&amp;q=QUANTITEPRODUIT&amp;p=PRIXPRODUIT" onclick="window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350'); return false;">Ajouter au panier</a>
    </body>
    </html>
    Fichiers attachés Fichiers attachés
    -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_SIGNATURE -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
    Dans la mesure du possible, essayez de montrer votre problème en situation réelle en nous donnant une url, que l'on puisse tester.
    Pensez également à cocher

    Aucun problème ne doit être résolu en MP (Message Privé) le forum est là pour ça.

    Dimension Internet

  3. #3
    telodo
    Invité(e)
    Par défaut
    Merci beaucoup de ton intervention !

    Je sais que le copier/coller est voue a l'echec dans la pluspart des cas. Malgres tout j'ai toujours tendance a d'abords tester le script et ensuite l'analyser de fond en comble pour le comprendre afin de le refaire moi meme selon mes besoins.

    Je te remercie encore une fois !

  4. #4
    telodo
    Invité(e)
    Par défaut
    Je remarque une premiere petite erreure.

    Lorsque j'ajoute une quantite de 5 par exemple. Le panier me le prends en double, donc 10.

    Ensuite lorsque je rafraichis la page, tout fonctionne. Cependant 2 lignes d'erreurs :

    Notice: Undefined index: l in C:\wamp\www\panier\panier.php on line 34

    Notice: Undefined index: p in C:\wamp\www\panier\panier.php on line 36
    Soit dans leurs contexte :

    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
     
    elseif(isset($_POST['action']))
    {
     unset($_GET);
     $erreur=false;
     
     if(in_array($_POST['action'],array('ajout', 'suppression', 'refresh')))
      $action=$_POST['action'];
     else
      $erreur=true;
     
      $l = preg_replace('#\v#', '',$_POST['l']); // ----- Ligne 34
     
      $p = floatval($_POST['p']); // ----- Ligne 36
     
     
     $QteArticle = array();
     
     $i=0;
     foreach ($_POST['QteArticle'] as $contenu){
      $QteArticle[$i++] = intval($contenu);
     
     }
     
    }
    De meme, lorsque je supprime un article tout fonctionne. Mais 2 lignes d'erreurs :


    Notice: Undefined index: q in C:\wamp\www\panier\panier.php on line 18

    Notice: Undefined index: p in C:\wamp\www\panier\panier.php on line 20
    Soit dans leurs contexte :

    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
     
    if (isset($_GET['action']))
    {
     $erreur=false;
     
     
     if(in_array( $_GET['action'],array('ajout', 'suppression', 'refresh')))
      $action = $_GET['action'];
     else
      $erreur=true;
     
     $l = preg_replace('#\v#', '', $_GET['l']);
     
     $q = intval($_GET['q']); // ----- Ligne 18
     
     $p = floatval($_GET['p']); // ----- Ligne 20
     
    }

    Je vais tenter de regarder de plus prêt pourquoi ces erreurs, mais si vous pouvez me mettre sur la voie avec une petite explication !

  5. #5
    telodo
    Invité(e)
    Par défaut
    SOLUTION :

    Je suis passe de WampServer a Xampp. Je n'ai a present plus aucun soucis !


    Merci pour l'aide que vous m'avez apporte pour le caddie. A present je vais etudier tout ca !

  6. #6
    telodo
    Invité(e)
    Par défaut
    Bon apparement Xampp ne suffit pas. Sur un PC ca fonctionne et sur l'autre non.

    Vive l'informatique.

Discussions similaires

  1. Yahoo! Pipes création flux avec un site en java
    Par Alexis05 dans le forum Webmarketing
    Réponses: 0
    Dernier message: 28/04/2013, 16h33
  2. Réponses: 0
    Dernier message: 18/04/2008, 02h36
  3. [CSS] Probleme avec l'affichage de mon site :
    Par vampyrx dans le forum Mise en page CSS
    Réponses: 1
    Dernier message: 28/08/2005, 23h23
  4. Création étiquettes avec signets dans Word
    Par rohstev dans le forum VBA Word
    Réponses: 2
    Dernier message: 18/03/2005, 13h14
  5. Création vue avec test d'existence
    Par yan77 dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 23/12/2004, 11h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo