Suppression ou modification impossible
Bonjour à tous,
Voilà 2 semaines que je m'arrive pas a résoudre un souci sur un script de panier.
L'ajout dans le panier fonctionne correctement mais la suppression ou la modification ne fonctionne pas du tout.
J'ai beau regarder en détail les fonctions et le script mais tout me parait correct.
A mon avis cela proviens de la page panier.php car le script avec les fonctions proviens d'un tuto.
Je vous fourni les scripts
Script des fonctions, page fonctions-panier.php
Code:
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
|
<?php
/**
* Verifie si le panier existe, le créé sinon
* @return booleen
*/
function creationPanier(){
if (!isset($_SESSION['paniers'])){
$_SESSION['paniers']=array();
$_SESSION['paniers']['libelleProduit'] = array();
$_SESSION['paniers']['qteProduit'] = array();
$_SESSION['paniers']['prixProduit'] = array();
$_SESSION['paniers']['verrou'] = false;
}
return true;
}
/**
* Ajoute un article dans le panier
* @param string $libelleProduit
* @param int $qteProduit
* @param float $prixProduit
* @return void
*/
function ajouterArticle($libelleProduit,$qteProduit,$prixProduit){
//Si le panier existe
if (creationPanier() && !isVerrouille())
{
//Si le produit existe déjà on ajoute seulement la quantité
$positionProduit = array_search($libelleProduit, $_SESSION['paniers']['libelleProduit']);
if ($positionProduit !== false)
{
$_SESSION['paniers']['qteProduit'][$positionProduit] += $qteProduit ;
}
else
{
//Sinon on ajoute le produit
array_push( $_SESSION['paniers']['libelleProduit'],$libelleProduit);
array_push( $_SESSION['paniers']['qteProduit'],$qteProduit);
array_push( $_SESSION['paniers']['prixProduit'],$prixProduit);
}
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
/**
* Modifie la quantité d'un article
* @param $libelleProduit
* @param $qteProduit
* @return void
*/
function modifierQTeArticle($libelleProduit,$qteProduit){
//Si le panier éxiste
if (creationPanier() && !isVerrouille())
{
//Si la quantité est positive on modifie sinon on supprime l'article
if ($qteProduit > 0)
{
//Recharche du produit dans le panier
$positionProduit = array_search($libelleProduit, $_SESSION['paniers']['libelleProduit']);
if ($positionProduit !== false)
{
$_SESSION['paniers']['qteProduit'][$positionProduit] = $qteProduit ;
}
}
else
supprimerArticle($libelleProduit);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
/**
* Supprime un article du panier
* @param $libelleProduit
* @return unknown_type
*/
function supprimerArticle($libelleProduit){
//Si le panier existe
if (creationPanier() && !isVerrouille())
{
//Nous allons passer par un panier temporaire
$tmp=array();
$tmp['libelleProduit'] = array();
$tmp['qteProduit'] = array();
$tmp['prixProduit'] = array();
$tmp['verrou'] = $_SESSION['panier']['verrou'];
for($i = 0; $i < count($_SESSION['paniers']['libelleProduit']); $i++)
{
if ($_SESSION['paniers']['libelleProduit'][$i] !== $libelleProduit)
{
array_push( $tmp['libelleProduit'],$_SESSION['paniers']['libelleProduit'][$i]);
array_push( $tmp['qteProduit'],$_SESSION['paniers']['qteProduit'][$i]);
array_push( $tmp['prixProduit'],$_SESSION['paniers']['prixProduit'][$i]);
}
}
//On remplace le panier en session par notre panier temporaire à jour
$_SESSION['panier'] = $tmp;
//On efface notre panier temporaire
unset($tmp);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
/**
* Montant total du panier
* @return int
*/
function MontantGlobal(){
$total=0;
for($i = 0; $i < count($_SESSION['paniers']['libelleProduit']); $i++)
{
$total += $_SESSION['paniers']['qteProduit'][$i] * $_SESSION['paniers']['prixProduit'][$i];
}
return $total;
}
/**
* Fonction de suppression du panier
* @return void
*/
function supprimePanier(){
unset($_SESSION['paniers']);
}
/**
* Permet de savoir si le panier est verrouillé
* @return booleen
*/
function isVerrouille(){
if (isset($_SESSION['paniers']) && $_SESSION['paniers']['verrou'])
return true;
else
return false;
}
/**
* Compte le nombre d'articles différents dans le panier
* @return int
*/
function compterArticles()
{
if (isset($_SESSION['paniers']))
return count($_SESSION['paniers']['libelleProduit']);
else
return 0;
}
?> |
Script de la page panier.php
Code:
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
<?php
if(isset($_COOKIE['langue'])){
switch ($_COOKIE['langue']) {
case fr:
require 'include/lang/fr.php';
break;
case en:
require 'include/lang/en.php';
break;
case ne:
require 'include/lang/ne.php';
break;
}
}else{
require 'include/lang/fr.php';
}
require 'include/includes.php';
$erreur = false;
$action = (isset($_POST['action'])? $_POST['action']: (isset($_GET['action'])? $_GET['action']:null )) ;
if($action !== null)
{
if(!in_array($action,array('ajout', 'suppression', 'refresh')))
$erreur=true;
//récuperation des variables en POST ou GET
$l = (isset($_POST['l'])? $_POST['l']: (isset($_GET['l'])? $_GET['l']:null )) ;
$p = (isset($_POST['p'])? $_POST['p']: (isset($_GET['p'])? $_GET['p']:null )) ;
$q = (isset($_POST['q'])? $_POST['q']: (isset($_GET['q'])? $_GET['q']:null )) ;
//Suppression des espaces verticaux
$l = preg_replace('#\v#', '',$l);
//On verifie que $p soit un float
$p = $p;
//On traite $q qui peut etre un entier simple ou un tableau d'entier
if (is_array($q)){
$QteArticle = array();
$i=0;
foreach ($q as $contenu){
$QteArticle[$i++] = intval($contenu);
}
}
else
$q = intval($q);
}
if (!$erreur){
switch($action){
Case "ajout":
ajouterArticle($l,$q,$p);
break;
Case "suppression":
supprimerArticle($l);
break;
Case "refresh" :
for ($i = 0 ; $i < count($QteArticle) ; $i++)
{
modifierQTeArticle($_SESSION['paniers']['libelleProduit'][$i],round($QteArticle[$i]));
}
break;
Default:
break;
}
}
?>
<!DOCTYPE html>
<!--[if IE 7]><html class="ie ie7"><![endif]-->
<!--[if IE 8]><html class="ie ie8"><![endif]-->
<!--[if IE 9]><html class="ie ie9"><![endif]-->
<html lang="en">
<head>
<title>Panier</title>
<!-- Fonts-->
<link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="plugins/jquery-bar-rating/dist/themes/fontawesome-stars.css">
<link rel="stylesheet" href="plugins/ps-icon/ps-icon.css">
<!-- CSS Library-->
<link rel="stylesheet" href="plugins/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="plugins/owl-carousel/assets/owl.carousel.css">
<link rel="stylesheet" href="plugins/jquery-bar-rating/dist/themes/fontawesome-stars.css">
<link rel="stylesheet" href="plugins/Magnific-Popup/dist/magnific-popup.css">
<link rel="stylesheet" href="plugins/jquery-ui/jquery-ui.min.css">
<link rel="stylesheet" href="plugins/bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css">
<link rel="stylesheet" href="plugins/slick/slick/slick.css">
<link rel="stylesheet" href="plugins/revolution/css/settings.css">
<link rel="stylesheet" href="plugins/revolution/css/layers.css">
<link rel="stylesheet" href="plugins/revolution/css/navigation.css">
<!-- Custom-->
<link rel="stylesheet" href="css/style.css">
<!--HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries-->
<!--WARNING: Respond.js doesn't work if you view the page via file://-->
<!--[if lt IE 9]><script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script><script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script><![endif]-->
</head>
<!--[if IE 7]><body class="ie7 lt-ie8 lt-ie9 lt-ie10"><![endif]-->
<!--[if IE 8]><body class="ie8 lt-ie9 lt-ie10"><![endif]-->
<!--[if IE 9]><body class="ie9 lt-ie10"><![endif]-->
<body class="page-init">
<?php require 'include/menu.php'; ?>
<div id="back2top"><i class="fa fa-angle-up"></i></div>
<div class="loader"></div>
<div class="page-wrap">
<!--section-->
<div class="ps-section--hero"><img src="images/hero/01.jpg" alt="">
<div class="ps-section__content text-center">
<h3 class="ps-section__title">Votre panier</h3>
<div class="ps-breadcrumb">
<ol class="breadcrumb">
<li><a href="index.php">Accueil</a></li>
<li class="active">Panier</li>
</ol>
</div>
</div>
</div>
<div class="ps-section--cart pt-100 pb-100">
<div class="container">
<div class="ps-cart-listing">
<p class="hidden-lg"><i></i></p>
<div class="table-responsive">
<form method="post" action="panier.php">
<table class="table">
<thead>
<tr>
<th>Produits</th>
<th>Prix</th>
<th>Quantité</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
if (creationPanier())
{
$nbArticles=count($_SESSION['paniers']['libelleProduit']);
if ($nbArticles <= 0){
}else
{
for ($i=0 ;$i < $nbArticles ; $i++)
{ ?>
<tr>
<td>
<div class="ps-product--cart"><?php echo htmlspecialchars($_SESSION['paniers']['libelleProduit'][$i]); ?></div>
</td>
<td><?php echo htmlspecialchars($_SESSION['paniers']['prixProduit'][$i]); ?></td>
<td>
<div class="form-group--number">
<!--<button class="minus"><span>-</span></button>-->
<input class="form-control" type="text" value="<?php echo htmlspecialchars($_SESSION['paniers']['qteProduit'][$i]); ?>"/>
<!--<button class="plus"><span>+</span></button>-->
</div>
</td>
<?php $total = $_SESSION['paniers']['prixProduit'][$i] * $_SESSION['paniers']['qteProduit'][$i]; ?>
<td><span class="total-row"><?php echo $total; ?></span></td>
<td>
<a class="ps-cart-listing__remove" href="panier.php?action=suppression&l=<?php echo rawurlencode($_SESSION['paniers']['libelleProduit'][$i]);?>"></a>
</td>
<?php }
?>
</tbody>
</table>
</div>
<div class="ps-cart__process">
<div class="row">
<div class="col-lg-8 col-md-8 col-sm-6 col-xs-12 ">
<div class="form-group form-group--icon ps-cart__promotion">
<div class="icon-wrap"><i class="fa fa-angle-right"></i>
<input class="ps-cart__shopping" type="submit" value="Rafraichir"/>
<input type="hidden" name="action" value="refresh"/>
</div>
</div>
<div class="form-groupform-order">
<button class="ps-cart__shopping">Continue mes achats</button>
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 ">
<div class="ps-cart__total">
<p>Prix total : <span><?php echo MontantGlobal(); ?></span></p>
<input class="ps-btn ps-btn--sm ps-btn--fullwidth" value="commande"/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
?>
</form> |
Merci d'avance pour votre aide