Insert summary when there is a change in a PHP Array

Asked

Viewed 92 times

2

to with a doubt that may be easy, but to broken the head, I have a table that is generated through an array, follows 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

generates the table below (photo):

tabela

as you can see has a column "Group", these groups go from 1 to 5, well, I would like each change in a group, had a row below with the summary of that group, as an average of production values, efficiency etc, but that’s not the problem, the problem is how to include this line with the summary to each group change, some idea, I thank you already!

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

sparse result would be the one of the photo below, done in excel, lines in yellow

tabela resultado

  • I confess that I did not understand, you want this to occur automatically, without the user give refresh on the page?

  • @Marcelo thanks for the return, the user filters by date, I want to come already separate in the query, at the time the user filters, no need to refresh, the image of the post reflects as it is today, but without the summary by group

  • You can create an if before the group to enter the values

  • Ahh... you want to make totals by group, rs

  • You create a variable before the start of the loop with the value of the first group, within the loop compare this variable with the current group, if it is different Oce inserts the total, after the loop you insert the last total, because it will not be possible inside the loop, I hope I was understanding.

  • @Marcelo understood yes, in the image there are only 2 groups, but there will be 5 groups, you suggest to compile the 5 groups into 5 variables before the loop? I added an image with the expected result too.

  • No, the variable receives the new group as soon as it changes to be compared with the next one again, I will put an example in the answer

Show 2 more comments

3 answers

1

I don’t know the data structure before this block you put so I’ll give you an example of what it would be like to total 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 did it in my 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 change Oce inserts the total and jumps to the next and so on, no matter the number of groups.

This scheme is the same as bank statement when grouping by date and the totals of each day

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

0

My suggestion is that you create an if before the group to compare the 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.