class de Panier
Bon bah jme faisai un peu chier staprem alors j'ai fait une class de panier. Elle est pas exceptionnelle, mais ca pourra peut etre vous aider a comprendre comment ca fonctionne :).
Si c'est trop long, mettez le dans un wall :)
Petite utilisation:
Si c'est trop long, mettez le dans un wall :)
- <?php
- /**
- Classe Panier
- Benjamin SLAKMON - slak@wanadoo.fr
- Création : 29/06/2005
- Modification : 29/06/2005
- */
- class Panier
- {
- var $panier = array();
- var $error = array();
- var $nbelts = 0;
- var $nbproduits = 0;
- /** Fonction permettant l'ajout d'un produit dans le panier
- * @param id Identifiant du produit
- * @param nb Nombre d'unités du produit que l'ont veut ajouter
- * @param prix prix du produit
- * @param descr description eventuelle du produit
- */
- function Add($id=0,$nb=1,$prix='',$descr='Aucun description pour ce produit')
- {
- if($id > 0)
- {
- if($nb > 0)
- {
- if(isset($this->panier[$id]))
- {
- $this->panier[$id]['qte'] += $nb;
- }
- else
- {
- $this->panier[$id]['descr'] = htmlentities($descr);
- $this->panier[$id]['qte'] = $nb;
- $this->panier[$id]['prix'] = round($prix,2);
- $this->nbproduits++;
- }
- $this->nbelts += $nb;
- if($this->panier[$id]['qte'] <= 0)
- {
- return $this->Del($id);
- }
- return TRUE;
- }
- else
- {
- $this->error[] = 'Impossible d\'ajouter un nombre inférieur ou égal à 0 d\'objet (1)';
- return FALSE;
- }
- }
- else
- {
- $this->error[] = 'L\'identifiant fourni en paramètre est invalide, il doit etre supérieur à 0 (1)';
- return FALSE;
- }
- }
- /** Fonction permettant d'enlever un nombre défini d'unités d'un produit
- * @param id Identifiant du produit
- * @param nb Nombre d'unités que l'ont veut enlever du produit id
- */
- function Rem($id=0,$nb=1)
- {
- if($id > 0)
- {
- if(isset($this->panier[$id]))
- {
- if( ($this->panier[$id]['qte'] - $nb) <= 0)
- {
- return $this->Del($id);
- }
- else
- {
- $this->panier[$id]['qte'] -= $nb;
- $this->nbelts -= $nb;
- }
- return TRUE;
- }
- else
- {
- $this->error[] = 'Ce produit n\'existe pas dans le panier (2)';
- return FALSE;
- }
- }
- else
- {
- $this->error[] = 'L\'identifiant fourni en paramètre est invalide, il doit etre supérieur à 0 (2)';
- return FALSE;
- }
- }
- /** Fonction permettant de supprimer un produit du panier
- * @param id Identifiant du produit
- */
- function Del($id=0)
- {
- if($id > 0)
- {
- if(isset($this->panier[$id]))
- {
- $this->nbelts -= $this->panier[$id]['qte'];
- unset($this->panier[$id]);
- $this->nbproduits--;
- return TRUE;
- }
- else
- {
- $this->error[] = 'Ce produit n\'existe pas dans le panier (3)';
- return FALSE;
- }
- }
- else
- {
- $this->error[] = 'L\'identifiant fourni en paramètre est invalide, il doit etre supérieur à 0 (3)';
- return FALSE;
- }
- }
- }
- ?>
Petite utilisation:
- <?php
- require_once 'class.Panier.php';
- session_start();
- if(!isset($_SESSION['panier']))
- {
- $_SESSION['panier'] = new Panier;
- }
- $_SESSION['panier']->Add(14,3,24.99,'Tour de 10 CD-R'); //Ajouter 3 objets '14' au panier
- $_SESSION['panier']->Add(14,13,24.99,'Tour de 10 CD-R'); // Ajouter 13 objets '14" au panier. Il y en a donc 16 (13+3)
- $_SESSION['panier']->Rem(14,4); // Enleve 4 objets 4 du panier, il en a donc plus que 12
- $_SESSION['panier']->Add(12,1,8.99,'Les indestructibles');
- $_SESSION->Del(14);
- ?>
Accès rapide :
Remonter 

