Donnée ne s'efface pas

Eléphant du PHP | 330 Messages

13 mars 2006, 00:55

Bonsoir,

Je crée un Caddie Virtuel (page caddie.php)
<?php

session_start();

include ("connection.php"); 

echo "$valeur"; 

//si on a quelque chose dans le panier
if(isset($_SESSION['panier']))
{
// on définit la variable $panier
$panier = $_SESSION['panier'];

?>
<div align="center"><H2>Votre Caddie contient les éléments suivants : </H2></div>
<br><br>
<table border="1" align="center" bgcolor="cccccc" width="50%">
<!-- En Têtes de Colonnes --> 
<tr bgcolor='white'>
<td width="20%" align="center" >Produit</td>
<td width="20%" align="center">Quantité</td>
<td width="20%" align="center">Px Unité (HT)</td>
<td width="20%" align="center">Total (HT)</td>
<td width="20%" align="center">Sélection&nbsp;</td>

</tr>
<!-- Création du Formulaire --> 
<form method="POST" action="commande.php">

<?
$sql="SELECT * FROM tbltauxtva";
$req_sql=mysql_query($sql)or exit ('Erreur SQL !'.$sql.'<br>'.mysql_error());
$taux_TVA=mysql_result($req_sql,0,"TVA_Montant");

foreach ($panier as $valeur=>$cde)
{

 
$sql="SELECT * FROM tblphotos WHERE Id='$valeur'";
$req=mysql_query($sql)or exit ('Erreur SQL !'.$sql.'<br>'.mysql_error());

//$sql_commande = "SELECT * FROM tblclients WHERE membre_Id=$membre_Id";
//$commande= mysql_query($sql_commande)or die ("Execution de la reqête impossible"); 
//$commande2=mysql_result($commande,0,"client_ID");


while( $data=mysql_fetch_array($req) )
{
$nom=$data['Index_Photo'];//référence de la photo
$prix=$data['Prixaveccadre'];// prix unitaire de la photo
$pxligne=$prix*$cde; //prix pour la ligne de commande cf $cde avant
$tot+=$pxligne;//valorisation du total général HT
$tot2=$tot;
$TVA=$tot2*$taux_TVA;//calcul de la TVA
$totTTC=$tot2+$TVA;//calcul du montant TTC

$pxligne=number_format($pxligne,2,".", " ");
$tot2=number_format($tot2,2,".", " ");
$TVA=number_format($TVA,2,".", " ");
$totTTC=number_format($totTTC,2,".", " ");

// on met la valeur de l'ID Photo dans la check Box
echo"<tr><td>$nom</td><td>$cde</td><td>$prix</td><td align='right'>$pxligne</td><td><input type='checkbox' name='case[]' value='".$data['Id']."'></td></tr>";

}
}

echo"<tr><td colspan='3' align='right'>Total Commandé...</td><td align='right'>$tot2</td></tr>";
echo"<tr><td colspan='3' align='right'>TVA (19.6 %)...</td><td align='right'>$TVA</td></tr>";
echo"<tr><td colspan='3' align='right'>Total TTC...</td><td align='right'>$totTTC</td></tr>";

$_SESSION['totalHT'] = $tot2;
$_SESSION['TVA'] = $TVA;
$_SESSION['totalTTC'] = $totTTC;

mysql_close();



?>

<tr bgcolor='white'>
<td colspan="5" align="center">Montants En Euros&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="action" value="Commander">&nbsp;&nbsp;&nbsp;<input type="submit" name="action" value="Eliminer">&nbsp;&nbsp;&nbsp;<input type="submit" name="action" value="Changer"></td></tr></table>
</form>

<br><br><a href="boutique.php"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ajouter une nouvelle référence à votre panier</strong></a>

<?
}
?> 
Je peux sélectioner une ligne pour l'annuler.

Si il y a une seule ligne, les montants s'effacent bien sauf pour la ligne concernant la TVA.

Pouvez vous me dire pourquoi.

Merci d'avance

Voici le code pour éliminer
<?php	

	}

// nous vérifions que le bouton Eliminer de la page "caddie" a bien été déclanché:
//dans ce cas nous allons supprimer une ou + ligne(s) de commande
if(isset($_POST['action']) and $_POST['action']=='Eliminer')
	{
// nous allons vérifier que la check box est bien cliquée
	if(empty($_POST['case']) )
		{
?>
		<script ="Javascript">
		history.go(-1);
		</script>
<?php
		exit;
		}
	foreach ($_POST['case'] as $delete)
		{
		unset($_SESSION['panier'][$delete]);
		}
	?>
	<script ="Javascript">
	window.location.replace('caddie.php');
	</script>
	<?php
	exit;
	}
?>

Mammouth du PHP | 19672 Messages

13 mars 2006, 01:20

Je suis pas sur d'avoir bien saisi, mais commencer par aligner proprement ton code en séparant le PHP du HTML, ça aiderait peut-être un peu à la lecture du code et à corriger au passage quelques erreurs :
<?php
session_start();
include ("connection.php");
echo $valeur;

//si on a quelque chose dans le panier
if(isset($_SESSION['panier']))
{
    // on définit la variable $panier
    $panier = $_SESSION['panier'];

?>
<div align="center"><H2>Votre Caddie contient les éléments suivants : </H2></div>
<br><br>
<!-- Création du Formulaire -->
<form method="POST" action="commande.php">
  <table border="1" align="center" bgcolor="cccccc" width="50%">
<!-- En Têtes de Colonnes -->
    <tr bgcolor='white'>
      <td width="20%" align="center">Produit</td>
      <td width="20%" align="center">Quantité</td>
      <td width="20%" align="center">Px Unité (HT)</td>
      <td width="20%" align="center">Total (HT)</td>
      <td width="20%" align="center">Sélection&nbsp;</td>
    </tr>
<?php
    $sql = "SELECT * FROM tbltauxtva";
    $req_sql = mysql_query($sql)or exit ('Erreur SQL !'.$sql.'<br>'.mysql_error());
    $taux_TVA = mysql_result($req_sql, 0, "TVA_Montant");
    
    foreach ($panier as $valeur => $cde)
    {
        $sql = "SELECT * FROM tblphotos WHERE Id='". $valeur ."'";
        $req = mysql_query($sql)or exit ('Erreur SQL !'.$sql.'<br>'.mysql_error());
    
        //$sql_commande = "SELECT * FROM tblclients WHERE membre_Id=$membre_Id";
        //$commande= mysql_query($sql_commande)or die ("Execution de la reqête impossible");
        //$commande2=mysql_result($commande,0,"client_ID");
    
        while( $data=mysql_fetch_array($req) )
        {
            $nom     = $data['Index_Photo'];    //référence de la photo
            $prix    = $data['Prixaveccadre'];  // prix unitaire de la photo
            $pxligne = $prix * $cde;            //prix pour la ligne de commande cf $cde avant
            $tot    += $pxligne;                //valorisation du total général HT
            $tot2    = $tot;
            $TVA     = $tot2 * $taux_TVA;       //calcul de la TVA
            $totTTC  = $tot2 + $TVA;            //calcul du montant TTC
    
            $pxligne = number_format($pxligne, 2, ".", " ");
            $tot2    = number_format($tot2, 2, ".", " ");
            $TVA     = number_format($TVA, 2, ".", " ");
            $totTTC  = number_format($totTTC, 2, ".", " ");
            // on met la valeur de l'ID Photo dans la check Box
?>
    <tr>
      <td><?php echo($nom); ?></td>
      <td><?php echo($cde); ?></td>
      <td><?php echo($prix); ?></td>
      <td align='right'><?php echo($pxligne); ?></td>
      <td><input type='checkbox' name='case[]' value='<?php echo($data['Id']); ?>'></td>
    </tr>
<?php
        }
    }
?>
    <tr>
      <td colspan='3' align='right'>Total Commandé...</td>
      <td colspan="2" align='right'><?php echo($tot2); ?></td>
    </tr>
    <tr>
      <td colspan='3' align='right'>TVA (19.6 %)...</td>
      <td colspan="2" align='right'><?php echo($TVA); ?></td>
    </tr>
    <tr>
      <td colspan='3' align='right'>Total TTC...</td>
      <td colspan="2" align='right'><?php echo($totTTC); ?></td>
    </tr>
<?php    
    $_SESSION['totalHT']  = $tot2;
    $_SESSION['TVA']      = $TVA;
    $_SESSION['totalTTC'] = $totTTC;
    
    mysql_close();
?>
    <tr bgcolor='white'>
      <td colspan="5" align="center">Montants En Euros&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="action" value="Commander">&nbsp;&nbsp;&nbsp;<input type="submit" name="action" value="Eliminer">&nbsp;&nbsp;&nbsp;<input type="submit" name="action" value="Changer"></td>
    </tr>
  </table>
</form>
<br><br>
<a href="boutique.php"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ajouter une nouvelle référence à votre panier</strong></a>
<?php
}
?>
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse :axe: