Sessions et système de panier
Bonjour!
J'essaie de faire un système de panier pour un site, mais je rencontre des problèmes avec les variables de sessions.
- En gros, avec $_GET je regarde si on veut ajouter/supprimer un article et son id correspondant dans la bdd.
- Ensuite j'appelle la fonction correspondante qui modifie $_SESSION['panier'].
- Quand je veux afficher le panier, je regarde les ids et les quantités dans $_SESSION['panier'] et je regarde dans la bdd pour savoir a quel article ça correspond.
Le problème, c'est que dans la fonction 'plus', $_SESSION['panier'] est bien modifié comme je le souhaite mais juste avant la boucle for avant l'affichage, il n'y a plus rien dans $_SESSION['panier'].
J'espère que quelqu'un pourra m'aider.
Voici le code:
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
|
<?php
session_start();
//connexion à la bdd avec PDO
if(!isset($_SESSION['panier']))
{
$_SESSION['panier'] = array();
}
if(isset($_GET['action']))
{
switch($_GET['action'])
{
case 'plus':
if(isset($_GET['id']))
{
plus(intval($_GET['id'])); //on veut ajouter l'article qui a l'id $_GET['id'] dans la bdd
}
//etc...
}
}
function in_panier($id) //verifie si l'article se trouve déja dans le panier
{
$present = -1;
for($i = 0; $i < sizeof($_SESSION['panier']); $i++)
{
if($_SESSION['panier'][$i]['id'] == $id)
{
$present = $i;
break;
}
}
return $present;
}
function plus($id)
{
global $bdd;
if(!is_int($id))
{
echo "Une erreur est survenue";
return;
}
$l = in_panier($id);
if($l != -1)
{
$_SESSION['panier'][$l]['qte']++;
echo 'in_panier';
}
else{
$req = $bdd->prepare('SELECT * FROM plats WHERE id = ?');
$req->bindValue(1, $id);
$req->execute();
while($r = $req->fetch())
{
$_SESSION['panier'][] = array(
'id' => intval($id),
'qte' => 1,
'prxtot' => floatval($r['prix']),
);
}
$req->closeCursor();
}
}
function minus($id)
{
//code
}
//On lis l'id et la quantité dans $_SESSION['panier'], et lis dans la bdd à quoi ça correspond et on met ces infos dans $panier
$panier = array();
for($i = 0; $i < sizeof($_SESSION['panier']); $i++)
{
$req = $bdd->prepare('SELECT * FROM plats WHERE id = ?');
if(is_int($_SESSION['panier'][$i]['id']))
{
$req->bindValue(1, $_SESSION['panier'][$i]['id']);
$req->execute();
while($l = $req->fetch())
{
$panier[] = array(
'fr' => $l['fr'],
'en' => $l['en'],
'qte' => $_SESSION['panier'][$i]['qte'],
'prxtot' => $_SESSION['panier'][$i]['qte'] * $l['prix'],
'id' => $_SESSION['panier'][$i]['id'],
);
}
}
else{
echo "Une erreur est survenue.";
}
}
//affichage du contenu du panier
?> |
Merci d'avance!