how can I add up within a datatable

0

I have the following code where I walk with a while but I want in each segment of a AREA to put a row with its total and then continue with the rest

Here my code

  $resultado = $conexion->query("SELECT Det.OP,Det.ID_Area,Det.Cliente,de.Desc_Area,
    sum(Det.Cantidad) as CantidadOP,
    sum(Det.Volumen) as VolumenOP,
    sum(Det.PrecioTotal) as PrecioTotalOP
    from Despacho_DetalleEntreAreas as Det
    inner join  Despacho_DescEntreAreas as de on de.ID_Area = Det.ID_Area  where Det.Fecha Between '$FDesde' and '$FHasta'
    Group by Det.OP, Det.ID_Area
    order by Det.ID_Area, Det.OP asc");
      while($row=$resultado->fetch_assoc())
        {
           $PrecioGrupo=$PrecioGrupo + $row['PrecioTotalOP']; ?>
          <tr>
            <td><?php echo $row['ID_Area'];?></td>
            <td><?php echo $row['Desc_Area'];?></td>
            <td><?php echo $row['OP'];?></td>
            <td><?php echo $row['Cliente'];?></td>
            <td><?php echo $row['CantidadOP'];?></td>
            <td><?php echo number_format($row['VolumenOP'],4,"." , ",");?></td>
            <td><?php echo "$". "". number_format($row['PrecioTotalOP'],0, " " , ",");?></td>
          </tr>

           }

my table:

  <table class="table table-bordered hover" id="Tabla_OpDespachadas_Areas" cellspacing="0">
            <thead>
              <tr>
                <th>ID_AREAS</th>
                <th>AREAS</th>
                <th>OP</th>
                <th>CLIENTE</th>
                <th>CANTIDAD</th>
                <th>VOLUMEN</th>
                <th>PRECIO</th>

              </tr>
            </thead>
            <tbody>
              <?php if (isset($_POST['Bto_Procesar'])): ?>
              <?php include("Clases/ProcesoOP.php") ?>
              <?php endif; ?>
            </tbody>

I enclose a photo as the code shows

I hope you have explained me well

Result:

    
asked by MoteCL 17.08.2018 в 19:47
source

1 answer

1

try to add everything in a loop and then respresentas, try this way:

//declaras una variable que funcionara como bandera para validar y almacenar el antiguo id
$id_Area=-1;
//declaras una variable que almacenara los resultados
$total=0;
//variable bandera en falso
$bandera = false;
while($row=$resultado->fetch_assoc())
{   
    if ($id_Area != $row['ID_Area'] && $id_Area != -1) $bandera =true;
   $PrecioGrupo=$PrecioGrupo + $row['PrecioTotalOP']; ?>
  <tr>
    <td><?php echo $row['ID_Area'];?></td>
    <td><?php echo $row['Desc_Area'];?></td>
    <td><?php echo $row['OP'];?></td>
    <td><?php echo $row['Cliente'];?></td>
    <td><?php echo $row['CantidadOP'];?></td>
    <td><?php echo number_format($row['VolumenOP'],4,"." , ",");?></td>
    <td><?php echo "$". "". number_format($row['PrecioTotalOP'],0, " " , ",");?></td>
  </tr>
<?php 
    //comparamos si el nuevo id es igual al viejo && verificamos que para evitar insertar en la primera instacia
    if ($bandera) {
    ?>
        <tr style="width: 100%;">
            <td style="text-align: right; width: 100%;"><?php echo $total;?></td>
        </tr>
    <?php 
        //ponemos la bandera en falso nuevamente
        $bandera= false;
        //reiniciamos la variable total
        $total = 0;
    }else{
        //dentro del ciclo y en cada vueltas vas sumando cuando no se cumpla la condicion
        $total = $total + number_format($row['PrecioTotalOP'],0, " " , ",");
    }
    //alamacenamos el valor del id para compararlo con la proxima vuelta
   $id_Area = $row['ID_Area'];
}
?>

I hope it serves you and mark it xD

    
answered by 17.08.2018 / 19:56
source