par
nicosjack » 11 mars 2014, 15:17
Merci pour ta réponse, voici le tuto que j'ai suivi et appliqué en entier :
http://www.grafikart.fr/tutoriels/php/p ... ession-309
Et voici le code de mon fichier panier.php qui affiche le récapitulatif de la commande :
<section id="main">
<a href="index.php" title="Retour à la page d'accueil">Retour</a>
<?php
$ids = array_keys($_SESSION['panier']);
//On récupère tous les produits ajoutés au panier
if(empty($ids)) {
$produits = array();
} else {
$produits = $DB->query('SELECT * FROM produits WHERE id_produit IN ('.implode(',', $ids).')');
}
?>
<form method="post" action="panier.php">
<table>
<thead>
<tr>
<th>Nom</th>
<th>Prix</th>
<th>Quantité</th>
<th>Sous-total</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($produits as $produit): ?>
<tr>
<td><?php echo $produit->nom_produit; ?></td>
<td><?php echo number_format($produit->prix_unitaire, 2, ',', ' '); ?> €</td>
<td><input class="spinner" type="number" name="panier[quantite][<?php echo $produit->id_produit; ?>]" value="<?php echo $_SESSION['panier'][$produit->id_produit]; ?>" /></td>
<td><?php echo number_format($produit->prix_unitaire * $_SESSION['panier'][$produit->id_produit], 2, ',', ' '); ?> €</td>
<td><a href="panier.php?delpanier=<?php echo $produit->id_produit; ?>" title="Supprimer le produit">Supprimer</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="submit" value="Recalculer" />
</form>
<strong>NOMBRE DE PRODUITS : <?php echo $panier->compter(); ?></strong>
<strong>TOTAL : <?php echo number_format($panier->total(), 2, ',', ' '); ?> €</strong>
<button>Valider la commande</button>
</section>
Et voici le code du fichier panier.class.php qui est appelé dans le header de la page :
<?php
class panier {
private $DB;
//Constructeur
public function __construct($DB) {
//S'il n'existe aucune session, on l'a créer
if(!isset($_SESSION)) {
session_start();
}
//Si aucun panier n'existe, on le créer
if(!isset($_SESSION['panier'])) {
$_SESSION['panier'] = array();
}
$this->DB = $DB;
if(isset($_GET['delpanier'])) {
$this->del($_GET['delpanier']);
}
if(isset($_POST['panier']['quantite'])) {
$this->recalc();
}
}
//Fonction permettant de recalculer le panier quand on change la quantité
public function recalc() {
foreach($_SESSION['panier'] as $product_id => $quantite) {
if(isset($_POST['panier']['quantite'][$product_id])) {
if(filter_var($_POST['panier']['quantite'][$product_id], FILTER_VALIDATE_INT) && $_POST['panier']['quantite'][$product_id] > 0) {
$_SESSION['panier'][$product_id] = $_POST['panier']['quantite'][$product_id];
}
else {
echo 'Attention, la quantité renseignée doit être un chiffre entier positif.';
}
}
}
}
//Fonction permettant de compter combien il y a de produits dans le panier
public function compter() {
return array_sum($_SESSION['panier']);
}
//Fonction permettant de calculer le prix total du panier
public function total() {
$total = 0;
$ids = array_keys($_SESSION['panier']);
//On récupère tous les produits ajoutés au panier
if(empty($ids)) {
$produits = array();
} else {
$produits = $this->DB->query('SELECT id_produit, prix_unitaire FROM produits WHERE id_produit IN ('.implode(',', $ids).')');
}
foreach($produits as $produit) {
$total += $produit->prix_unitaire * $_SESSION['panier'][$produit->id_produit];
}
return $total;
}
//Fonction permettant d'ajouter un produit au panier
public function add($product_id) {
//Si le panier contient déjà ce produit
if(isset($_SESSION['panier'][$product_id])) {
//On en ajoute un
$_SESSION['panier'][$product_id]++;
} else {
//Sinon on en créer un
$_SESSION['panier'][$product_id] = 1;
}
}
public function del($product_id) {
unset($_SESSION['panier'][$product_id]);
}
}
?>
Merci pour ta réponse, voici le tuto que j'ai suivi et appliqué en entier : [url]http://www.grafikart.fr/tutoriels/php/panier-php-session-309[/url]
Et voici le code de mon fichier panier.php qui affiche le récapitulatif de la commande :
[php]<section id="main">
<a href="index.php" title="Retour à la page d'accueil">Retour</a>
<?php
$ids = array_keys($_SESSION['panier']);
//On récupère tous les produits ajoutés au panier
if(empty($ids)) {
$produits = array();
} else {
$produits = $DB->query('SELECT * FROM produits WHERE id_produit IN ('.implode(',', $ids).')');
}
?>
<form method="post" action="panier.php">
<table>
<thead>
<tr>
<th>Nom</th>
<th>Prix</th>
<th>Quantité</th>
<th>Sous-total</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($produits as $produit): ?>
<tr>
<td><?php echo $produit->nom_produit; ?></td>
<td><?php echo number_format($produit->prix_unitaire, 2, ',', ' '); ?> €</td>
<td><input class="spinner" type="number" name="panier[quantite][<?php echo $produit->id_produit; ?>]" value="<?php echo $_SESSION['panier'][$produit->id_produit]; ?>" /></td>
<td><?php echo number_format($produit->prix_unitaire * $_SESSION['panier'][$produit->id_produit], 2, ',', ' '); ?> €</td>
<td><a href="panier.php?delpanier=<?php echo $produit->id_produit; ?>" title="Supprimer le produit">Supprimer</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="submit" value="Recalculer" />
</form>
<strong>NOMBRE DE PRODUITS : <?php echo $panier->compter(); ?></strong>
<strong>TOTAL : <?php echo number_format($panier->total(), 2, ',', ' '); ?> €</strong>
<button>Valider la commande</button>
</section>[/php]
Et voici le code du fichier panier.class.php qui est appelé dans le header de la page :
[php]<?php
class panier {
private $DB;
//Constructeur
public function __construct($DB) {
//S'il n'existe aucune session, on l'a créer
if(!isset($_SESSION)) {
session_start();
}
//Si aucun panier n'existe, on le créer
if(!isset($_SESSION['panier'])) {
$_SESSION['panier'] = array();
}
$this->DB = $DB;
if(isset($_GET['delpanier'])) {
$this->del($_GET['delpanier']);
}
if(isset($_POST['panier']['quantite'])) {
$this->recalc();
}
}
//Fonction permettant de recalculer le panier quand on change la quantité
public function recalc() {
foreach($_SESSION['panier'] as $product_id => $quantite) {
if(isset($_POST['panier']['quantite'][$product_id])) {
if(filter_var($_POST['panier']['quantite'][$product_id], FILTER_VALIDATE_INT) && $_POST['panier']['quantite'][$product_id] > 0) {
$_SESSION['panier'][$product_id] = $_POST['panier']['quantite'][$product_id];
}
else {
echo 'Attention, la quantité renseignée doit être un chiffre entier positif.';
}
}
}
}
//Fonction permettant de compter combien il y a de produits dans le panier
public function compter() {
return array_sum($_SESSION['panier']);
}
//Fonction permettant de calculer le prix total du panier
public function total() {
$total = 0;
$ids = array_keys($_SESSION['panier']);
//On récupère tous les produits ajoutés au panier
if(empty($ids)) {
$produits = array();
} else {
$produits = $this->DB->query('SELECT id_produit, prix_unitaire FROM produits WHERE id_produit IN ('.implode(',', $ids).')');
}
foreach($produits as $produit) {
$total += $produit->prix_unitaire * $_SESSION['panier'][$produit->id_produit];
}
return $total;
}
//Fonction permettant d'ajouter un produit au panier
public function add($product_id) {
//Si le panier contient déjà ce produit
if(isset($_SESSION['panier'][$product_id])) {
//On en ajoute un
$_SESSION['panier'][$product_id]++;
} else {
//Sinon on en créer un
$_SESSION['panier'][$product_id] = 1;
}
}
public function del($product_id) {
unset($_SESSION['panier'][$product_id]);
}
}
?>[/php]