Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1063 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  [PHP/SQL] somme en php de deux colonnes SQL

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[PHP/SQL] somme en php de deux colonnes SQL

n°2443887
casimir92
Posté le 30-03-2023 à 21:55:48  profilanswer
 

Bonjour à tous,
J'ai besoin d'aide sur une page que j'essaye d'écrire, j'ai un tableau dans une table avec plusieurs montants correspondant a personne1, personne2, personne3après voici mon code qui ne fonctionne pas parfaitement et je coince pour résoudre mes problèmes

Code :
  1. <table>
  2.                 <tr>
  3.                     <th class="">Produits</th>
  4.                     <th class="">Personne1</th>
  5.                     <th class="">Personne2</th>
  6.                     <th class="">Personne3</th>
  7.                     <th class="">Personne4</th>
  8.                 </tr>
  9.                 <?php
  10.                 //création de la requéte "Séries Open" SQL
  11.                 $reqAllF = $conn->prepare('SELECT id,member,produit,montant_personne1,montant_personne2,montant_personne3,montant_personne4, SUM(montant_personne1) AS total_personne1 FROM tb_f ORDER BY produit ASC');
  12.                 $reqAllF->execute();
  13.                 $allF = $reqAllF->fetchAll();
  14.                 foreach ($allF as $line)
  15.                     {
  16.                         echo "<tr>";
  17.                             echo "<td class=produit>".$line['produit']."</td>";
  18.                             echo "<td class=montant>".$line['montant_personne1']." €</td>";
  19.                             echo "<td class=montant>".$line['montant_personne2']." €</td>";
  20.                             echo "<td class=montant>".$line['montant_personne3']." €</td>";
  21.                             echo "<td class=montant>".$line['montant_personne4']." €</td>";                   
  22.                         echo "</tr>";
  23.                     }
  24.                     echo "<tr>";
  25.                         echo "<td></td>";
  26.                         echo "<td class=montant>".$line['total_personne1']." €</td>";
  27.                         echo "<td class=montant>".$line['total_personne2']." €</td>";
  28.                         echo "<td class=montant>".$line['total_personne3']." €</td>";
  29.                     echo "<td class=montant>".$line['total_personne4']." €</td>";                     
  30.                 echo "</tr>";
  31.                 ?>
  32.             </table>


Je pense que mon problème vient de ma requête SQL qui n'est pas formatée correctement, mais je comprend pas comment faire pour réaliser plusieurs somme dans la même requête.Si vous avez des idées


Message édité par casimir92 le 30-03-2023 à 22:17:42
mood
Publicité
Posté le 30-03-2023 à 21:55:48  profilanswer
 

n°2443928
casimir92
Posté le 31-03-2023 à 13:03:31  profilanswer
 

Je me réponds à moi-même
 

Code :
  1. <table
  2.     <tr>
  3.         <th class="">Produits</th>
  4.         <th class="">Personne1</th>
  5.         <th class="">Personne2</th>
  6.         <th class="">Personne3</th>
  7.         <th class="">Personne4</th>
  8.     </tr>
  9. <?php
  10. //création de la requéte SQL
  11. $reqAllF = $conn->prepare('SELECT * FROM tb_f ORDER BY produit ASC');
  12. $reqAllF->execute();
  13. $allF = $reqAllF->fetchAll();
  14. $total = [
  15.     'personne1' => 0
  16.     'personne2' => 0
  17.     'personne3' => 0
  18.     'personne4' => 0
  19. ];
  20. foreach ($allF as $line)
  21.     $total['personne1'] += $line['montant_personne1'];
  22.     $total['personne2'] += $line['montant_personne2'];
  23.     $total['personne3'] += $line['montant_personne3'];
  24.     $total['personne4'] += $line['montant_personne4'];
  25.     echo "<tr>";
  26.         echo "<td class=produit>".$line['produit']."</td>";
  27.         echo "<td class=montant>".number_format($line['montant_personne1'], 2, ',', ' ')." €</td>";
  28.         echo "<td class=montant>".number_format($line['montant_personne2'], 2, ',', ' ')." €</td>";
  29.         echo "<td class=montant>".number_format($line['montant_personne3'], 2, ',', ' ')." €</td>";
  30.         echo "<td class=montant>".number_format($line['montant_personne4'], 2, ',', ' ')." €</td>";                   
  31.     echo "</tr>";
  32. }
  33. echo "<tr>";
  34.     echo "<td></td>";
  35.     echo "<td class=montant>".number_format($total['personne1'], 2, ',', ' ')." €</td>";
  36.     echo "<td class=montant>".number_format($total['personne2'], 2, ',', ' ')." €</td>";
  37.     echo "<td class=montant>".number_format($total['personne3'], 2, ',', ' ')." €</td>";
  38.     echo "<td class=montant>".number_format($total['personne4'], 2, ',', ' ')." €</td>";                     
  39. echo "</tr>";
  40. ?>
  41. </table>


 
On m'a conseillé de gérer le calcul des totaux par PHP plutôt que dans la requête SQL, se qui me convient


Message édité par casimir92 le 31-03-2023 à 13:47:04
n°2443955
gregs11
Posté le 31-03-2023 à 17:08:16  profilanswer
 

Salut,  
 
Tu peux aussi utiliser les fonctions analytiques  :D  
exemple :

Code :
  1. SELECT id,
  2.   member,
  3.   produit,
  4.   montant_personne1,
  5.   montant_personne2,
  6.   montant_personne3,
  7.   montant_personne4,
  8.   SUM(montant_personne1) OVER () AS total_personne1,
  9.   SUM(montant_personne2) OVER () AS total_personne2,
  10.   SUM(montant_personne3) OVER () AS total_personne3,
  11.   SUM(montant_personne4) OVER () AS total_personne4 
  12. FROM tb_f
  13. ORDER BY produit ASC


 


---------------
Mon topic Achat/Ventes/Dons
n°2443967
casimir92
Posté le 31-03-2023 à 18:54:17  profilanswer
 

gregs11 a écrit :

Salut,  
 
Tu peux aussi utiliser les fonctions analytiques  :D  
exemple :


Code :
  1. SELECT id,
  2.   member,
  3.   produit,
  4.   montant_personne1,
  5.   montant_personne2,
  6.   montant_personne3,
  7.   montant_personne4,
  8.   SUM(montant_personne1) OVER () AS total_personne1,
  9.   SUM(montant_personne2) OVER () AS total_personne2,
  10.   SUM(montant_personne3) OVER () AS total_personne3,
  11.   SUM(montant_personne4) OVER () AS total_personne4 
  12. FROM tb_f
  13. ORDER BY produit ASC


J'étais effectivement parti sur cette piste suite a quelques recherches Internet


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  [PHP/SQL] somme en php de deux colonnes SQL

 

Sujets relatifs
je connais SQL, mais c'est quoi un site PHP/MYSQL et à quoi sert php?Requete SQL (PostgreSQL)
[problème] Php Ajax refresh, Js eventPHP - Récupérer le rating dans les exif d'une photo
[EXCEL]somme de valeurs associées à une date sans fonctions matriciellPHP rentre 0 au lieu de NULL
Microservices : Go, Swoole ou Php Natif ?Saut de ligne code PHP
SQL meilleure solution pour une BDD ? 
Plus de sujets relatifs à : [PHP/SQL] somme en php de deux colonnes SQL


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)