Insert summary when there is a change in a PHP Array

2

To with a doubt that may be easy, but I have broken the head, I have a table that is generated through an array, follow the code below:

    foreach($grupo_arr $k => $v){

  ?>  
  <tr>
    <td><?php echo $num_array[$k]; ?></td>
    <td><?php echo $maq_array[$k]; ?></td>
    <td><?php echo $grupo_arr[$k]; ?></td>
    <td><?php echo $producao_array[$k]; ?></td>
    <td><?php echo (($producao_array[$k]) * 2); ?></td>
    <td><?php echo $segunda_array[$k]; ?></td>
    <td><?php echo $terceira_array[$k]; ?></td>
    <td><?php echo $producao_r_array[$k]; ?></td>
    <td><?php echo (($producao_r_array[$k])*2); ?></td>
    <td><?php echo $eficiencia_array[$k]."%"; ?></td>
  </tr>
  <?php } //fecha foreach

generate the table below (photo):

Asyoucanseethereisa"Group" column, these groups range from 1 to 5, well, I would like every change in a group to have a row below the summary of that group, as an average of the production values , efficiency etc, but this is not the problem, the problem is how to include this line with the summary with each change of group, some idea, thank you right away!

Context: the user filters by date, what I want to do is that it already comes separated in the query, at the time that the user filters, does not need to refresh, the image of the post reflects as it is today, but without the summary by group

This is the result of the photo below, done in excel, yellow lines

    
asked by anonymous 08.06.2018 / 15:10

3 answers

1

I do not know the data structure before that block that you put so I'll give an example of how it would be to totalize by group using a query

...
$SuaQuery->Execute();
$Row = $SuaQuery->fetch(PDO::FETCH_ASSOC); // Inicio o Row

$subtotal = 0;

while($Row){ // Enquanto houver registro em Row
  $grupo = $Row["grupo"]; // Pega grupo
  do {
    // Aqui o seu html por linha
    echo("valor item: ". $Row["valoritem"]."<br>");   
    $subtotal += $Row["valoritem"];
    $Row = $SuaQuery->fetch(PDO::FETCH_ASSOC); // Avanca registro

  }while($grupo == $Row["grupo"]); // Enquanto for mesmo grupo

  if ($grupo != $Row["grupo"]){
    // Aqui coloca o html dos totais do grupo anterior
    echo($subtotal."<br>");
    $subtotal = 0; // Zera subtotal para o proximo grupo

  }
}

I made the head to pass the idea, the idea is you enter the loop with the first group, insert line until the group does not change, when you change you enter the total and jump next and so on, no matter what number of groups.

This scheme is the same as bank statement when grouped by date and totals for each day

    
08.06.2018 / 16:03
0

I believe it works.

foreach($grupo_arr $k => $v){
    $c1 += $producao_array[$k];
    $c2 += $segunda_array[$k];
    $c3 += $terceira_array[$k];
    $c4 += $producao_r_array[$k];
    $c5 += $eficiencia_array[$k];

    $ln ++ ;
   if($grupoant == grupo_arr[$k]){
   ?>

 <tr style="background:#ffff00">
    <td> </td>
    <td> </td>
    <td> </td>
    <td><?php echo $c1; ?> </td>
    <td><?php echo $c1*2; ?> </td>
    <td><?php echo $c2; ?> </td>
    <td><?php echo $c3; ?> </td>
    <td><?php echo $c4; ?></td>
    <td><?php echo $c4*2; ?></td>

    <td><?php echo $c5 / $ln; ?> </td>
  </tr>
   <?php

    $c1 =0;
    $c2 = 0;
    $c3 = 0;
    $c4 = 0;
    $c5 = 0;
    } else {

  ?>  
  <tr>
    <td><?php echo $num_array[$k]; ?></td>
    <td><?php echo $maq_array[$k]; ?></td>
    <td><?php echo $grupo_arr[$k]; ?></td>
    <td><?php echo $producao_array[$k]; ?></td>
    <td><?php echo (($producao_array[$k]) * 2); ?></td>
    <td><?php echo $segunda_array[$k]; ?></td>
    <td><?php echo $terceira_array[$k]; ?></td>
    <td><?php echo $producao_r_array[$k]; ?></td>
    <td><?php echo (($producao_r_array[$k])*2); ?></td>
    <td><?php echo $eficiencia_array[$k]."%"; ?></td>
  </tr>
  <?php
 }

$grupoant = $grupo_arr[$k];  
} //fecha foreach
    
08.06.2018 / 16:12
0

My suggestion is you create an if before group to compare values

Example:

$qtde  = 0;
$grupo = 0;
$soma = 0;
foreach($grupo_arr $k => $v){

  ?>  
  <tr>
    <td><?php echo $num_array[$k]; ?></td>
    <td><?php echo $maq_array[$k]; ?></td>
    <td><?php echo $grupo_arr[$k]; ?></td>
    <td><?php echo $producao_array[$k]; ?></td>
    <td><?php echo (($producao_array[$k]) * 2); ?></td>
    <td><?php echo $segunda_array[$k]; ?></td>
    <td><?php echo $terceira_array[$k]; ?></td>
    <td><?php echo $producao_r_array[$k]; ?></td>
    <td><?php echo (($producao_r_array[$k])*2); ?></td>
    <td><?php echo $eficiencia_array[$k]."%"; ?></td>       
  </tr>
 <?php
  if( $grupo > 0 ){
        if( grupo == $grupo_arr[$k] ){
          $qtde++;
          $soma =+ $producao_array[$k];
        }else{
          $media = soma / qtde;
          $qtde = 0;
          $soma = 0;
    ?>
        <tr> 
           <td>Total </td><td><?php echo $media; ?></td>
        </tr>
    <?php     

        }//fim do if interno
    } //fim so if
    $grupo = $grupo_arr[$k];
  <?php } //fim do for

Then you add the other options

    
08.06.2018 / 16:20