Tuto Panier : page panier.php

Par macinsteph, il y a 9 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.
Je n'arrive pas à récupérer les produits de ma base de données.

<?php require 'header.php';?> <div class="container"> <div class="row"> <div class="col-lg-12"> <h2>Votre panier</h2> <table class="panierTab"> <thead> <tr> <th>Produit</th> <th>Libéllé du produit</th> <th>Description</th> <th>Prix HT</th> <th>quantité</th> <th>Prix TTC</th> <th>Sous-total</th> <th>Action</th> </tr> </thead> <?php $ids = array_keys($_SESSION['panier']); $products = $DB->query('SELECT * FROM products WHERE id IN ('.implode(',',$ids).')'); foreach ($products as $product) : ?> <tbody> <tr> <td><img src="img/min/1.jpg"></td> <td><?php echo $product->name; ?></td> <td><?php echo $product->description; ?></td> <td><?php echo number_format($product->price,2,',',' '); ?> €</td> <td>1</td> <td><?php echo number_format($product->price * 1.196,2,',',' '); ?> €</td> <td>37</td> <td><a href="#"><img src="img/poubelle.png"></a></td> </tr> </tbody> <?php endforeach; ?> </table> <div class="rowTotal"> Total de votre commande : <span class="total">2654,25 €</span> </div> </div> </div> </div> <?php require 'footer.php';?>Entourez votre code pour bien le mettre en forme

J'ai fais un var_dump $_SESSION ça me donne : array(1) { ["panier"]=> array(0) { } }
var_dump de $products il me retourne array(0) { }
un var_dump de $ids il retourne array(0) { }.
comprends pas ??

merci

45 réponses

Sparkosis, il y a 9 ans
if (session_status() == PHP_SESSION_NONE) { session_start(); } else { die(print_r($_SESSION['panier'])); }

essaie d'ajouter un produit de nouveau et recommence

macinsteph, il y a 9 ans

rien du tout

Sparkosis, il y a 9 ans

hum étrange, c'est comme si il essayé de start la session à chaque fois.

if (session_status() == PHP_SESSION_NONE) { session_start(); die(print_r($_SESSION)); } else { die(print_r($_SESSION['panier'])); }
macinsteph, il y a 9 ans

ça donne ça : Array ( ) 1

Sparkosis, il y a 9 ans

Hum, en effet c'est un soucis de session. elle ne se propage pas entre les pages, creer une page à la racine en l'appelant n'importe comment et ajoute ça:

session_start(); print_r($_SESSION['panier']);

si elle te réponds rien c'est qu'elle se détruit quelque part

Sparkosis, il y a 9 ans

Salut,
il semblerait que lorsque tu ajoute un article à ton panier, la session ne s'initialise pas.

Peux-tu nous afficher le code de ton ajout d'article ?

macinsteph, il y a 9 ans

ma page d'accueil ou il y a les articles :

<?php require 'header.php'; ?> <div class="container"> <div class="row"> <?php $products = $DB->query('SELECT * FROM products'); ?> <?PHP foreach ($products as $product): ?> <div class="col-sm-4 col-lg-4 col-md-4"> <div class="thumbnail"> <img src="img/produits/<?php echo $product->id;?>.jpeg" alt=""> <div class="caption"> <h4 class="pull-right"><?php echo number_format($product->price,2,',',' '); ?> €</h4> <h4><a href="#"><?php echo $product->name; ?></a> </h4> <p><?php echo $product->description; ?></p> </div> <div class="details"> <a class="btn btn-primary" href="details.php?id=<?php echo $product->id;?>">Détails</a> </div> <div class="ratings"> <p class="pull-right">15 reviews</p> <p> <span class="glyphicon glyphicon-star"></span> <span class="glyphicon glyphicon-star"></span> <span class="glyphicon glyphicon-star"></span> <span class="glyphicon glyphicon-star"></span> <span class="glyphicon glyphicon-star"></span> </p> </div> <div class="box-footer"> <a href="addpanier.php?id=<?php echo $product->id;?>" class="add"><img src="img/market.png"></a> </div> </div> </div> <?php endforeach; ?> </div> </div> <?php require 'footer.php'; ?>

voilà

macinsteph, il y a 9 ans

et la page addpanier.php :

<?php require '_header.php'; if (isset($_GET['id'])) { $product = $DB->query('SELECT id FROM products WHERE id = :id', array('id' => $_GET['id'])); if(empty($product)){ die("Ce produit n'existe pas"); } $panier->add($product[0]->id); die('Le produit a bien été ajouté à votre panier, <a href="javascript:history.back()">retourner sur le catalogue</a>'); }else{ die("Vous n'avez pas séléctionné de produit à ajouter au panier !"); } ?>

merci

Sparkosis, il y a 9 ans

Ton call à la méthode

$panier->add($product[0]->id);

panier est une instance d'une class, envoie moi la méthode correspondante; Si il y a aucun $_SESSION['panier'], il te la manque

macinsteph, il y a 9 ans

mon fichier panier.class.php :

<?php class panier{ public function __construct(){ if (!isset($_SESSION)){ session_start(); } if (!isset($_SESSION['panier'])) { $_SESSION['panier'] = array(); } } public function add($product_id){ $_SESSION['panier'][$product_id] = 1; } } ?>
Sparkosis, il y a 9 ans

Peut tu rajouter un var_dump($_SESSION['panier']) aprés ton $panier->add($product[0]->id);
et essayer de rajouter un article (voir si ta méthode fonctionne correctement)

macinsteph, il y a 9 ans

ça donne ça :

array(1) { [1]=> int(1) }
macinsteph, il y a 9 ans

en message il me l'a bien ajouté, mais en physique rien

Sparkosis, il y a 9 ans

donc apprioris ta méthode est ok vu qu'il renvoie (je suppose l'article n°1)

Retourne sur la page qui "bug" est rajoute en dessous de ton require

if (session_status() == PHP_SESSION_NONE) { die("La session est pas set"); } else { die(print_r($_SESSION['panier']); }

et dis moi le retour

macinsteph, il y a 9 ans

mon code change de couleur quand je rajoute ton code

Sparkosis, il y a 9 ans

Pour moi c'est juste un probléme de session entre t'es différentes pages, essaie de faire ce que je t'ai mis plus haut. Par contre j'ai vu tu as un require ('_header.php'); et require ('header.php'); normal ?

EDIT: ton code change de couleur sur l'ide tu veux dire ? j'ai oublié une parenthèse ")" dans le deuxieme die ^^"

macinsteph, il y a 9 ans

oui c'est normal pour le header.php
j'ai corrigé je teste et je donne la réponse

macinsteph, il y a 9 ans

il me retourne : La session est pas set

Sparkosis, il y a 9 ans

Donc à la place du die('session est pas set'); met un session_start(); et dis moi le retour

macinsteph, il y a 9 ans

comme ça :

if (session_status() == PHP_SESSION_NONE) { session_start(); } else { die(print_r($_SESSION['panier']); }

ça donne rien

macinsteph, il y a 9 ans

rien page blanche

Sparkosis, il y a 9 ans

A mon avis lorsque tu ajoute ton article, elle sauvegarde en session, mais lorsque tu change de page, cette session ne se propage pas, ce qui veut dire qu'elle doit forcément être supprimé quelque part, dans ton header peut être ?

macinsteph, il y a 9 ans

je vais contrôler
merci

macinsteph, il y a 9 ans

j'ai contrôlé pas de problème ce que côté.
J'ai fais un <?php var_dump($_SESSION); ?> dans la page panier et ça me donne ça : array(1) { ["panier"]=> array(0) { } }
il me récupère bien le panier mais le tableau est vide ??

Sparkosis, il y a 9 ans

As tu rajouter des articles dans ton panier ?

macinsteph, il y a 9 ans

oui deux fois

Sparkosis, il y a 9 ans

Hum essaie d'initialisé la session dans le header et non dans ta class

macinsteph, il y a 9 ans

salut ça ne fonctionne pas mieux, la je séche !!

Carouge10, il y a 9 ans

Bonsoir,
Tu travailles en local ? En ligne ?
Type de serveur ?
Le code est crée 100% par toi ou c'est d'un script pris ailleurs que tu complètes ?

macinsteph, il y a 9 ans

salut
je travail en local
sur MAMP Apache
Je tente de refaire le tuto du panier de grafikart simplement en essayant de comprendre.

Carouge10, il y a 9 ans

d'accord. N'as-tu pas un "session_destroy" qui arriverais ?

macinsteph, il y a 9 ans

non rien mais quand je teste ma session je n'ai rien en retour

macinsteph, il y a 9 ans

j'ai fais toutes les manip de Sparkosis et rien

Carouge10, il y a 9 ans

C'est le premier projet que tu fais avec Mamp ?

macinsteph, il y a 9 ans

non pas du tout tout fonctionnait jusque là.

Carouge10, il y a 9 ans

Une mise à jour récente ?

macinsteph, il y a 9 ans

dès que je suis arrivé au choix des articles et les ajouter au panier avec l'utilisation des sessions ça plante

macinsteph, il y a 9 ans

j'ai réinstallé MAMP j'ai eu un soucis avec Mysql server

Carouge10, il y a 9 ans

Que dises les logs de mamp ?

macinsteph, il y a 9 ans

je te mets les dernières erreurs log php :

[07-Jun-2017 23:00:09 Europe/Berlin] PHP Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 in /Applications/MAMP/htdocs/panier/db.class.php on line 31
[07-Jun-2017 23:00:09 Europe/Berlin] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/panier/db.class.php:42) in /Applications/MAMP/htdocs/panier/panier.class.php on line 6
[07-Jun-2017 23:05:03 Europe/Berlin] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/panier/db.class.php:42) in /Applications/MAMP/htdocs/panier/panier.class.php on line 6
[07-Jun-2017 23:05:03 Europe/Berlin] PHP Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 in /Applications/MAMP/htdocs/panier/db.class.php on line 31
[07-Jun-2017 23:05:11 Europe/Berlin] PHP Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 in /Applications/MAMP/htdocs/panier/db.class.php on line 31
[07-Jun-2017 23:05:16 Europe/Berlin] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/panier/db.class.php:42) in /Applications/MAMP/htdocs/panier/panier.class.php on line 6
[07-Jun-2017 23:05:24 Europe/Berlin] PHP Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 in /Applications/MAMP/htdocs/panier/db.class.php on line 31

macinsteph, il y a 9 ans

le dernier en ajoutant au panier :

[07-Jun-2017 23:23:33 Europe/Berlin] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/panier/db.class.php:42) in /Applications/MAMP/htdocs/panier/panier.class.php on line 6

Carouge10, il y a 9 ans

Donc déjà, il y a un session_start(); de trop.

macinsteph, il y a 9 ans

c'est bon j'ai trouvé le problème, j'avais laissé des espaces après le ?> dans le fichier db.class.php !!!!
oufffff !!!
un grand merci à vous les gars !!!!!!!

Sparkosis, il y a 9 ans

Particulier comme bug x) un conseille ne ferme jamais php quand tu fais des class :)

macinsteph, il y a 9 ans

C clair !!!!