Calcul ligne par ligne dans un tableau PHP - MySQL
RésoluAstolpho Messages postés 73 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
J'ai un problème de calcul quand je veut afficher un total ligne par ligne dans un tableau, j'ai un champs "price" que je multiplie par la quantité "quantity" ça fonctionne sauf que dès que j'ai plus d'une ligne avec plus de 1 quantité le résultat est faux, j'arrive pas a m'en sortir voici mon code pour afficher :
require_once('../inc/connexion.php'); $db = getDB(); try { } catch (PDOException $e) { die('Erreur : ' . $e->getMessage()); } $stmt = $db->query(" SELECT a.nfacture, a.prixtotal, a.quantity, a.promo, a.customers, a.start_date, a.summary, a.state, a.numero, b.paid FROM (SELECT invoices.invoice as nfacture, SUM(services.price) as prixtotal, quantity as quantity, promotions.amount as promo, customers.fullname as customers, DATE_FORMAT(start_date,'%d/%m/%Y') as start_date, states.state as state, invoices.numero as numero, comments.summary as summary FROM invoices LEFT JOIN customers ON (invoices.id_customers=customers.id_customers) LEFT JOIN services ON (invoices.invoice=services.invoice) LEFT JOIN promotions ON (invoices.invoice=promotions.invoice) LEFT JOIN items ON (services.id_items=items.id_items) LEFT JOIN states ON (invoices.invoice=states.invoice) LEFT JOIN comments ON (invoices.invoice=comments.invoice) WHERE states.type = 1 AND (states.state = 1 OR states.state = 9) GROUP BY nfacture ) as a, (SELECT invoices.invoice as nfacture, SUM(payments.amount) as paid FROM invoices LEFT JOIN payments ON (invoices.invoice=payments.invoice) GROUP BY nfacture ) as b WHERE a.nfacture=b.nfacture ORDER BY nfacture DESC "); echo ' <div class="card"> <div class="card-header"> <h3 class="card-title">Factures</h3> </div> <!-- /.card-header --> <div class="card-body"> <table id="example1" class="table table-hover table-striped table-responsive"> <thead> <tr> <th>ID</th> <th >Numéro</th> <th>Client</th> <th>Titre</th> <th>Price</th> <th>Paid</th> <th>Balance</th> <th>Date</th> <th>Statut</th> </tr> </thead> <tbody> '; while ($donnees = $stmt->fetch()) // Renvoit les valeurs de la bdd { $quantity = $donnees['quantity']; $promo = $donnees['promo']; $paid = $donnees['paid']; $prix = $donnees['prixtotal'] * $donnees['quantity'] ; echo $prix; // echo $tax['vat']; echo '<br>'; // echo $donnees['prixtotal']; echo '<br>'; // echo $donnees['quantity']; echo '<br>'; // echo $donnees['promo']; echo '<br>'; $total = $prix - $promo; // $balance = $total - $paid; // $total = $prixtotal - $promo - $paid ; // $balance = $total - $promo - $paid; $state = ($donnees['state']); if ($donnees['state'] == 1) { $state = '<span class="badge bg-warning" >En cours</span>'; } elseif ($donnees['state'] == 2) { $state = '<span class="badge bg-success" >Payé</span>'; } elseif ($donnees['state'] == 9) { $state = '<span class="badge bg-danger" >Annulée</span>'; } echo '<tr> <td align="center" width="5%" ><a href="' . URLSITE . 'form/invoice.php?invoice=' . $donnees['nfacture'] . ' "><button type="button" class="btn btn-block btn-secondary btn-xs btn-flat">' . $donnees['nfacture'] . '</td></button></a> <td align="left" width="6%">' . $donnees['numero'] . '</td> <td align="left" width="30%">' . $donnees['customers'] . '</td> <td align="left" width="30%">' . $donnees['summary'] . '</td>'; echo '<td align="left" width="5%">' . number_format($total, 2) . ' </td>'; echo '<td align="left" width="5%"></></td>'; echo '<td align="left" width="5%"></td>'; echo '<td align="left" width="5%"> ' . $donnees['start_date'] . ' </td>'; echo '<td align="left" width="5%"> ' . $state . ' </td>'; } echo ' </tbody>'; echo '</tr> </tbody> <tfoot> <tr> <th>ID</th> <th >Numéro</th> <th>Client</th> <th>Titre</th> <th>Price</th> <th>Paid</th> <th>Balance</th> <th>Date</th> <th>Statut</th> </tr> </tfoot> </table> </div> <!-- /.card-body --> </div> <!-- /.card --> </div> <!-- /.col --> </div> <!-- /.row --> </div>'; echo ' </table> </div> <!-- /.card-body --> </div> <!-- /.card --> </div> <!-- /.col --> </div> <!-- /.container-fluid --> '; ?>
si quelqu'un pouvait m'aider à trouver la solution.
Merci
- Calcul ligne par ligne dans un tableau PHP - MySQL
- Partager photos en ligne - Guide
- Tableau word - Guide
- Mètre en ligne - Guide
- Aller à la ligne excel - Guide
- Trier un tableau excel - Guide
8 réponses
Bonjour,
Déjà, pour le prix par ligne, pas besoin du php .. tu peux le gérer directement dans ta requête SQL
par exemple
SELECT a.nfacture, a.prixtotal, a.quantity, a.promo, a.customers, a.start_date, a.summary, a.state, a.numero, b.paid , a.prixtotal * a.quantity as PRIX_LIGNE
Ensuite, pour le total du tableau, tu as oublié d'additionner les valeurs précédentes à ta variable $total
Avec ma modif dans ta requête sql; ça donnerait :
$prix = $donnees['PRIX_LIGNE'] $total += $prix
Bonjour Jordane45,
J'ai toujours le même résultat 580 au lieu de 420 j'ai :
1 article à 100 avec 2 quantité = 200
1 article à 160 avec 1 quantité = 160
1 article à 30 avec 2 quantité = 60
En tout ça devrait faire 420 et non 580, il y à 160 de plus ça correspond à l'article qui vaut 160.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionJ'ai fait cette requête :
SELECT a.nfacture, SUM(services.price * a.quantity) - SUM(promotions.amount * a.quantity) as prixtotal, a.quantity, a.promo, a.customers, DATE_FORMAT(a.start_date,'%d/%m/%Y') as start_date, a.summary, a.state, a.numero, b.paid, (services.price * a.quantity) - (promotions.amount * a.quantity) as PRIX_LIGNE, services.price FROM (SELECT invoices.invoice as nfacture, services.id_items, quantity as quantity, SUM(services.price) as prixtotal, SUM(promotions.amount) as promo, customers.fullname as customers, start_date, states.state as state, invoices.numero as numero, comments.summary as summary FROM invoices LEFT JOIN customers ON (invoices.id_customers=customers.id_customers) LEFT JOIN services ON (invoices.invoice=services.invoice) LEFT JOIN promotions ON (invoices.invoice=promotions.invoice AND services.id_items = promotions.id_items) LEFT JOIN items ON (services.id_items=items.id_items) LEFT JOIN states ON (invoices.invoice=states.invoice) LEFT JOIN comments ON (invoices.invoice=comments.invoice) WHERE states.type = 1 AND (states.state = 1 OR states.state = 9) GROUP BY nfacture, services.id_items ) as a, (SELECT invoices.invoice as nfacture, SUM(payments.amount) as paid FROM invoices LEFT JOIN payments ON (invoices.invoice=payments.invoice) GROUP BY nfacture ) as b, services WHERE a.nfacture=b.nfacture AND services.invoice = a.nfacture AND services.id_items = a.id_items ORDER BY nfacture DESC
j'ai le message :
Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Champ 'promotions.amount' inconnu dans field list
alors que le champs amount existe bien dans la table promotions
cette requete foncionne :
SELECT invoices.invoice as nfacture, SUM(services.price * services.quantity) as prixtotal, services.quantity as quantity, (SELECT SUM(promotions.amount) FROM promotions WHERE promotions.invoice = services.invoice AND promotions.id_items = services.id_items) as promo, states.state as state, invoices.numero as numero, SUM(payments.amount) as paid, SUM(services.price * services.quantity) - IFNULL((SELECT SUM(promotions.amount) FROM promotions WHERE promotions.invoice = services.invoice AND promotions.id_items = services.id_items), 0) as PRIX_LIGNE FROM invoices LEFT JOIN services ON (invoices.invoice=services.invoice) LEFT JOIN states ON (invoices.invoice=states.invoice) LEFT JOIN payments ON (invoices.invoice=payments.invoice) WHERE states.type = 1 AND (states.state = 1 OR states.state = 9) GROUP BY nfacture ORDER BY nfacture DESC