[RESOLU] problème de calcul de prix total

Eléphant du PHP | 63 Messages

23 janv. 2015, 22:59

bonjour,
mon problème est le suivant:
je n'arrive pas à faire le calcul de la somme global de mon devis
voici mon bout de code

Code : Tout sélectionner

echo '<form method="POST" action="f2.php>"<center><table id="devis"> <tr><th> Ref </th><th width=200> Dénomination </th><th width=100> Prix unitaire </th><th width=100> Quantitée </th><th width=100> Prix </th></tr>'; if(isset($_POST['idProduit'])) { foreach ($_POST['idProduit'] as $clef=>$idProduit) { // REQUÊTE SQL de récupération des informations du produit<br> $sql = "SELECT * FROM produit WHERE id_produit = '$idProduit'"; $req = mysql_query($sql) or die (mysql_error()); $recup = mysql_fetch_assoc($req); // affichage d'une ligne $quantite=$_POST['quantite'][$clef]; echo'<tr><td>'.$idProduit.'</td><td>'.$recup['nom_produit'].'</td><td>'.$recup['prix_produit'].'</td>'; echo'<td>'.$quantite.'</td>'; echo '<td>'; $prix = $recup['prix_produit']; $t = $prix * $quantite; echo $t; echo '</td></tr>'; } } echo'<tr><td colspan=3></td><td><b>TOTAL:</b></td><td>'; $total = 0; $total += $t; echo $total; echo'</td></tr></table></center>';
pouvez vous m'aider sur ce sujet

Mammouth du PHP | 688 Messages

23 janv. 2015, 23:56

$total = 0;
à mettre avant la boucle

$total += $t;
à mettre à l'intérieur de la boucle, une fois que $t a la bonne valeur.

Eléphant du PHP | 63 Messages

24 janv. 2015, 00:31

yes ça fonctionne
un gros merci