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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| <?php
class CPanier {
// Données membres
public $panier = array(array());
// Constructeur
function __construct() {
if (!isset($_SESSION['panier'])) $_SESSION['panier'] = array();
$this->panier =& $_SESSION['panier'];
}
// Methodes
public function ajouteArticle($ref,$qte=1){
$infos = $this->voirInfos($ref);
@$this->panier[$ref]['quantite'] += $qte;
@$this->panier[$ref]['reference'] = $ref;
@$this->panier[$ref]['photo'] = $infos['pic'];
@$this->panier[$ref]['titre'] = $infos['nom'];
@$this->panier[$ref]['prix'] = $infos['prix'];
@$this->panier[$ref]['poids'] = $infos['poids'];
@$this->panier[$ref]['cat'] = $infos['cat'];
if ($qte <= 0) unset ($this->panier[$ref]);
}
public function supprimeArticle($ref='',$qte=1){
@$this->panier[$ref]['quantite'] -= $qte;
if ($this->panier[$ref]['quantite'] <= 0) unset ($this->panier[$ref]);
}
public function changeQuantite($ref='',$toSet=''){
@$this->panier[$ref]['quantite'] = $toSet ;
if ($this->panier[$ref]['quantite'] <= 0) unset ($this->panier[$ref]);
}
public function voirQuantite($ref=''){
if ($ref) {
return $this->panier[$ref]['quantite'];
}
else {
$total = 0;
foreach($this->panier as $ref => $data) {
$total += $data['quantite'];
}
return $total;
}
}
public function voirInfos($ref) { // Affiche le prix d'un article, sinon le total (sans ref)
if (!is_null($ref)) $ref = 'WHERE `id`='. $ref;
$select = 'SELECT `id`,`prix`,`poids`,`photo`,`titre`,`categorie` FROM '. DB_PREFIX .'catalogue '. $ref;
$query = mysql_query($select);
$row = mysql_fetch_row($query);
return array(
'prix' => $row[1],
'poids' => $row[2],
'ref' => $row[0],
'pic' => $row[3],
'nom' => $row[4],
'cat' => $row[5]
);
}
public function voirPanier(){
$list = array();
$i = 0;
foreach($this->panier as $ref => $data) {
$list['ref'][$i] = $ref;
$list['pic'][$i] = $data['photo'];
$list['nom'][$i] = $data['titre'];
$list['qte'][$i] = $data['quantite'];
$list['prx'][$i] = $data['prix'];
$list['pds'][$i] = $data['poids'];
$list['cat'][$i] = $data['cat'];
$i++;
}
return $list;
}
}
?> |
Partager