Ver Mensaje Individual
  #5 (permalink)  
Antiguo 26/03/2021, 17:12
pilucho
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
Respuesta: sumar el total de todo los items

intento numero 2 y nada me de vuelve el mismo total de cada ítem, y no suma el total de todo los ítems.
obteniendo el total de todo los items como ejemplo, ya se puede seguir con los de mas... pero nada aun.

sale_report.php
Código PHP:
Ver original
  1. <?php
  2.             $year = date('Y');
  3.             $servername = "localhost";
  4.             $username = "username";
  5.             $password = "password";
  6.             $conn = new mysqli($servername, $username, $password);
  7.             if ($conn->connect_error) {  die("Connection failed: " . $conn->connect_error); } echo "Connected successfully";
  8.             if(isset($_POST['submit'])){
  9.             // me podrian recomendar alguna idea como validar?
  10.             }
  11.             $startdate = $_POST['startdate'];
  12.             $enddate = $_POST['enddate'];
  13.  
  14.             $sql  = "SELECT s.sFecha,p.nameProducts,s.vClient,p.sale_price,p.buy_price,
  15.            COUNT(s.product_id) AS total_records,
  16.            SUM(s.gty_s) AS total_qty,
  17.            SUM(p.sale_price * s.gty_s) AS total_s_price,
  18.            SUM(p.buy_price * s.gty_s) AS total_buy_price
  19.            FROM sales s
  20.            LEFT JOIN products p ON s.product_id = p.product_id
  21.            WHERE s.sFecha BETWEEN '{$startdate}' AND '{$enddate}'
  22.            GROUP BY DATE(s.sFecha),p.nameProducts
  23.            ORDER BY DATE(s.sFecha) DESC";
  24.  
  25.             $result = $conn->query($sql);
  26.             if ($result->num_rows > 0) {
  27.             // AQUI    
  28.             $total_del_todo_ganancias = 0;
  29.             $sum = 0;
  30.             $sub = 0;
  31.             while($row = $result->fetch_assoc()) {
  32.             /* O AQUI IGUAL NO SUMA EL TOTAL DE TODO
  33.             $total_del_todo_ganancias = 0;
  34.             $sum = 0;
  35.             $sub = 0;
  36.             */
  37.             ?>
  38.  
  39.             <?php
  40.             // INTENTO 2
  41.             $sum += $row['total_saleing_price'];
  42.             $sub += $row['total_buying_price'];
  43.             $ganancias = $sum - $sub;
  44.             echo "-> $ganancias  <-"; // EL MISMO TOTAL DE CADA ITEM'S y NO el total de TODO
  45.             ?>
  46.  
  47.             <?php
  48.             $ganancias  = $total_s_price - $total_buy_price ;
  49.             echo number_format($ganancias,2)."<br>";
  50.             // INTENTO 1
  51.             $ganancias  = $total_s_price - $total_buy_price ;
  52.             $total_del_todo_ganancias = $total_del_todo_ganancias +$ganancias  ;
  53.             ?>
  54.             <?php
  55.             }
  56.             } else {
  57.             echo "0 result";
  58.             }
  59.             $conn->close();
  60. ?>