Notice: Undefined offset: 1 in C:\wamp\www\Nouveau dossier\panier.php on line 23

etincelle01 Messages postés 10 Date d'inscription dimanche 4 octobre 2009 Statut Membre Dernière intervention 27 mai 2010 - 13 mai 2010 à 17:39
kohntark Messages postés 3705 Date d'inscription lundi 5 juillet 2004 Statut Membre Dernière intervention 27 avril 2012 - 15 mai 2010 à 11:50
Bonjour,

J'essaie de faire un panier en php en utilisant les sessions.
J'ai une page page panier.php qui affiche les produits ajoutés au panier, mais j'obtiens l'erreur suivante:
Notice: Undefined offset: 1 in C:\wamp\www\Nouveau dossier\panier.php on line 22
Notice: Undefined offset: 1 in C:\wamp\www\Nouveau dossier\panier.php on line 26
Voici mon code:
<?php
session_start(); 
$n = count($_SESSION['panier']); 
if($n != 0) {
$n++; 
}
if ((isset($_POST['id']))and (isset($_POST['quantite'])))
{
$_SESSION['panier'][$n]['id']=$_POST['id']; 
$_SESSION['panier'][$n]['quantite'] = $_POST['quantite'];
}

$n = count($_SESSION['panier']);
for ($i = 0; $i<$n; $i++) {
?>


<?php
echo $_SESSION['panier'][$i]['id'];
?>


<?php
echo $_SESSION['panier'][$i]['quantite'];
?>


<?php

}
?>


Les lignes 22 et 26 sont les suivantes:
echo $_SESSION['panier'][$i]['id'];
echo $_SESSION['panier'][$i]['quantite'];

Merci d'avance pour toute aide.

1 réponse

kohntark Messages postés 3705 Date d'inscription lundi 5 juillet 2004 Statut Membre Dernière intervention 27 avril 2012 30
15 mai 2010 à 11:50
Salut,

J'ai lu rapidement mais :
Lorsque qu'il n'y a rien dans le panier :

$n count($_SESSION['panier']); // 0
if($n != 0) { // non exécuté
   $n++; 
}

if ((isset($_POST['id']))and (isset($_POST['quantite']))) {
  // == $_SESSION['panier'][0]['id']
  $_SESSION['panier'][$n]['id']=$_POST['id']; 
  
  // == $_SESSION['panier'][0]['quantite']
  $_SESSION['panier'][$n]['quantite'] = $_POST['quantite']; 
}
// == 1 article dans le panier



Lors de l'ajout d'un autre article :
$n count($_SESSION['panier']); // 1
if($n != 0) { // exécuté
   $n++; // $n = 2
}

if ((isset($_POST['id']))and (isset($_POST['quantite']))) {
  // == $_SESSION['panier'][2]['id']
  $_SESSION['panier'][$n]['id']=$_POST['id']; 
  
  // == $_SESSION['panier'][2]['quantite']
  $_SESSION['panier'][$n]['quantite'] = $_POST['quantite']; 
}
// == 2 articles dans le panier

for ($i 0; $i<$n; $i++) { // for ($i = 0; $i<2; $i++)
  // l'index 1 n'existe pas !! (index existants : 0 et 2)
}



Cordialement,

Kohntark -
0
Rejoignez-nous