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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
<?php
// Les fonctions
function createPanier(){
try
{
$bdd = new PDO('mysql:host=localhost;dbname=projet;charset=utf8', 'root', '');
$bdd->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); // Les noms de champ seront en minuscule
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Les erreurs lanceront des excetions
}
catch(Exception $e){
die("Une erreur est survenue");
}
if(isset($_SESSION['panier'])){
$_SESSION['panier'] = array();
$_SESSION['panier']['libelleProduit'] = array();
$_SESSION['panier']['qteProduit'] = array();
$_SESSION['panier']['prixProduit'] = array();
$_SESSION['panier']['verrou'] = false;
$select = $bdd->query("SELECT TVA FROM produit");
$data = $select->fetch(PDO::FETCH_OBJ);
$_SESSION['panier']['tva'] = $data->tva;
}
return true;
}
// la fonction d'ajout de produit au panier
function ajouterProduit($libelleProduit,$qteProduit,$prixProduit){
if(createPanier() && !isVerrouille())
{
$positionProduit = array_search($libelleProduit, $_SESSION['panier']['libelleProduit']);
if($positionProduit !== false)
{
$_SESSION['panier']['qteProduit'][$positionProduit] += $qteProduit;
}
else
{
array_push($_SESSION['panier']['libelleProduit'],$libelleProduit);
array_push($_SESSION['panier']['qteProduit'],$qteProduit);
array_push($_SESSION['panier']['prixProduit'],$prixProduit);
}
}
else{
// c'estle message qui s'affiche
echo 'Erreur!! Veuillez contacter l\'administrateur ajouterProduit';
}
}
if(!$erreur){
//L'erreur pourrait venir de là
switch ($action){
Case "ajout":
//Appel à la fonction d'ajout
ajouterProduit($l,$q,$p);
break;
Case "suppression":
supprimerProduit($l);
break;
Case "refresh":
for($i=0;$i<count($qteProduit);$i++){
modifierQteProduit($_SESSION['panier']['libelleProduit'][$i], round($qteProduit));
}
break;
Default:
break;
}
}
?>
<form method="post" action="">
<table width="400">
<tr>
<td colspan="4">Votre panier</td>
</tr>
<tr>
<td>Libelle du Produit</td>
<td>Prix unitaire</td>
<td>Quantité</td>
<td>TVA</td>
<td>Action</td>
</tr>
<?php
if(isset($_GET['deletepanier']) && $_GET['deletepanier'] == true){
supprimerPanier();
}
if(createPanier()){
$nbProduit = count($_SESSION['panier']['libelleProduit']);
if($nbProduit <= 0){
echo "Oups votre panier est vide!!";
}else{
//j'ai des supçons sur cette "for" aussi
for($i=0; $i<$nbProduit; $i++){
?>
<tr>
<td><br/><?php echo $_SESSION['panier']['libelleProduit'][$i]; ?></td>
<td><br/><?php echo $_SESSION['panier']['prixProduit']['$i']; ?></td>
<td><br/><input name="q[]" value="<?php echo $_SESSION['panier']['qteProduit']['$i']; ?>" size="5"/></td>
<td><br/><?php echo $_SESSION['panier']['tva']."%"; ?></td>
<td><br/><a href="panier.php?action=suppression& l=<?php echo rawurlencode($_SESSION['panier']['libelleProduit'][$i]); ?>">X</a></td>
</tr>
<?php } ?>
<tr>
<td colspan="2"><br/>
<p>Total: <?php echo montantGlobal(); ?></p>
<p>Total avec tva: <?php echo montantGlobalTva(); ?></p>
</td>
</tr>
<tr>
<td colspan="4">
<input type="submit" value="rafraichir" />
<input type="hidden" name="action" value="refresh" />
<a href="?deletepanier=true">Supprimer le panier</a>
</td>
</tr>
<?php
}
}
?>
</table>
</form> |
Partager