bonjour, je travaille sur un site e-commerce j'ai un problème dans le panier
quand j'ajoute des produits au panier je peux les modifier ou supprimer sauf le premier dans le panier
voici le code
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
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
167
168
169
170
171
172
173
174
175
<?php
 
/**
 * Verifie si le panier existe, le créé sinon
 * @return booleen
 */
/**
 * 
 */
class panier{
 
function creationPanier(){
   if (!isset($_SESSION['panier'])){
 
 
      $_SESSION['panier']=array();
      $_SESSION['panier']['libelleProduit'] = array();
      $_SESSION['panier']['qteProduit'] = array();
      $_SESSION['panier']['prixProduit'] = array();
     // $_SESSION['panier']['verrou'] = false;
    //  $_SESSION['panier']['time'] = time();
       $_SESSION['time'] = time();
 
   }
   return true;
}
 
 
/**
 * Ajoute un article dans le panier
 * @param string $libelleProduit
 * @param int $qteProduit
 * @param float $prixProduit
 * @return void
 */
function ajouterArticle($libelleProduit,$qteProduit,$prixProduit){
 
   //Si le panier existe
 
      //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 ;
         // $_SESSION['panier']['time'] = time();
         $_SESSION['time'] = time();
      }
      else
      {
         //Sinon on ajoute le produit
         array_push( $_SESSION['panier']['libelleProduit'],$libelleProduit);
         array_push( $_SESSION['panier']['qteProduit'],$qteProduit);
         array_push( $_SESSION['panier']['prixProduit'],$prixProduit);
        // $_SESSION['panier']['time'] = time();
          $_SESSION['time'] = time();
      }
   }
 
 
 
 
/**
 * 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
 
         //Recharche du produit dans le panier
         $positionProduit = array_search($libelleProduit,  $_SESSION['panier']['libelleProduit']);
 
         if ($positionProduit !== false)
         {
            $_SESSION['panier']['qteProduit'][$positionProduit] = $qteProduit ;
           // $_SESSION['panier']['time'] = time();
            $_SESSION['time'] = time();
         }
      }
 
 /*  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['verrou'] = $_SESSION['panier']['verrou'];
  //    $tmp['time'] = $_SESSION['panier']['time'] = time();
 
      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]);
         }
 
      }
      //On remplace le panier en session par notre panier temporaire à jour
      $_SESSION['panier'] =  $tmp;
      $_SESSION['time'] = time();
     return $_SESSION['panier'];
      //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;
 
}
}
?>
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
if (isset($_GET["action"]))
  {session_start();
        if ($_GET["action"] == "supp")
    {
    	if (isset($_GET['libelle'])) {
    		# code...
    		require_once'fonction_panier.php';
    		$panel=new panier();
    		$pn=$panel->creationPanier();
 
 	if ($pn) {
echo $_GET['libelle'];
     		$pan=$panel->supprimerArticle($_GET['libelle']);
    	//	echo "supprimeeeeeeeeeeeeeeeeeeeeeeeer";
     		header('location: panier1.php');
 
    	}
 
 
 
    }}elseif ($_GET["action"] == "update") {
    	# code...
    	echo "string";
    	if (isset($_GET['libele'])) {
    		$qte=$_POST['quantity'];
    		//echo $qte;
 
			require_once'fonction_panier.php';
    		$panel=new panier();
    		$pn=$panel->creationPanier();
 
 			if ($pn) {
 
    		$pan=$panel->modifierQTeArticle($_GET['libele'],$qte);
    		echo "cbn qte 1 modifié";
    		echo $qte;
    		// header('location: panier1.php');
 
 
    }
}}elseif ($_GET["action"] == "vider") {
	# code...
	require_once'fonction_panier.php';
    		$panel=new panier();
    		$pn=$panel->creationPanier();
 
 			if ($pn) {
 
    		$pan=$panel->supprimePanier();
    		 header('location: panier1.php');
 
}
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
<table class="table-shopping-cart" id="messages">
 
  <tr class="table_head">
                  <th class="column-1">article</th>
                  <th class="column-2"></th>
                  <th class="column-3">Prix</th>
                  <th class="qty">Quantite</th>
                  <th class="column-5">Supprimer</th>
                </tr>
                <?php
            require("fonction_panier.php");
                $pan=new panier();
                $crp=$pan->creationPanier();
  if ($crp)
  {
    $nbArticles=count($_SESSION['panier']['libelleProduit']);
 
    if ($nbArticles <= 0)echo "<tr><td>Votre panier est vide </ td></tr>";
    else
    {
      for ($i=0 ; $i< $nbArticles ; $i++)
      {?>
                <tr class="table_row">
                  <td class="column-1">
                    <div class="how-itemcart1">
                      <img src="images/item-cart-04.jpg" alt="IMG">
                    </div>
                  </td>
 
                  <td class="column-2"> <?php echo $_SESSION['panier']['libelleProduit'][$i];?></td>
                  <td class="column-3"><?php echo $_SESSION['panier']['prixProduit'][$i]; ?></td>
 
                      <td class="qty">
                                            <div class="qty-btn d-flex">
 
                                                <div class="quantity">
                                                    <span class="qty-minus" style="cursor: pointer;" onclick="var effect = document.getElementById('qty'); var qty = effect.value; if( !isNaN( qty ) &amp;&amp; qty &gt; 1 ) effect.value--;return false;"><i class="fa fa-minus" aria-hidden="true"></i></span>
                                                   <form action="teste.php?action=update&libele=<?php echo $_SESSION['panier']['libelleProduit'][$i];?>" method="post">
                                                    <input type="number" class="qty-text" id="qty" step="1" min="1" max="300" name="quantity" value="<?php echo $_SESSION['panier']['qteProduit'][$i] ;?>">
 
                                                    <button>update</button>
                                                    </form>
                                                    <span class="qty-plus" style="cursor: pointer;" onclick="var effect = document.getElementById('qty'); var qty = effect.value; if( !isNaN( qty )) effect.value++;return false;"><i class="fa fa-plus" aria-hidden="true"></i></span>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="column-5"><a href="teste.php?action=supp&amp;libelle=<?php echo $_SESSION['panier']['libelleProduit'][$i]; ?>"><i class="fa fa-times fa-lg" aria-hidden="true"></i>supprimer</a></td>
                </tr>
               <?php } }}?> 
</table>