E commerce Panier calculer le montant

Bonjour,
j'ai réalisé un panier de E com, j'ai de problème pour finir le calculer le montant total, il n'arrive pas faire l'addition des montants précédents, seulement afficher le montant partial, qq'un peut m'aider de modifier mon script ? svp!

<?php
session_start();
require_once 'panier.php';
$panier = new Panier('produits');
$listproduit = $panier->getPanier();
?>
<?php if(!$listproduit){?>
<p> votre panier est vide </p>
<?php } else {?>
<table border="1" width="50%">
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
<td>Montant</td>
</tr>
<?php foreach($listproduit as $produit) {
$totalprice=0;
$subtotal=$produit['quantity']*$produit['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php print $produit['name'] ?></td>
<td><?php print $produit['price'] ?></td>
<td><?php print $produit['quantity'] ?></td>
<td><?php print($produit['quantity']*$produit['price'])?>€</td>
</tr>
<?php } ?>
<?php echo "Total: $totalprice";?>€
</table>

<?php } ?>
<p><a href="index.php">Accueil</a></p>

3 réponses

  • Votes
  • Date
  • Date décroissante
  1. Bonjour,

    C'est normal :
    <?php
    foreach($listproduit as $produit) {
        $totalprice=0; 

    À chaque itération de la boucle, tu remets $totalprice à 0 !

    Il faut inverser ces deux lignes.

    Xavier
    1. merci Xavier.
  2. merci,
    vous avez raison, le calcul est bon,
    mais un autre problème ce qu'il mémorise l'ancienne quantité( et montant aussi),
    donc, comment effacer les anciennes valeurs chaque fois je recommence ?
    1. au départ mettez début et après chaque instruction mettez fin "instruction en question" puis à la fin mettez fin boucle.