Sum foreach within foreach

Asked

Viewed 683 times

-1

I’m having a question about why the sum inside the foreach isn’t working.

I solved the situation differently by recreating a new select with Count(*).

But I’d like to know if I’m doing something wrong, or if I can do it another way using that same structure.

The situation is this:

The main foreach searches all deliverers that exist in the order table.

The second foreach brings only forms of payment where certain couriers are associated .

I would like to know why the code below is not working, since when performing a var_dump($Totqtdeformreturn) instead of displaying the TOTAL 15 , it displays the content values I need to add .

foreach($EntregasResumo_rs = $sqlEntregas_res->fetchAll(PDO::FETCH_ASSOC) as $Entregas_rs){
            echo "<tr>";
            echo "<td width='3px' >".$Entregas_rs['IdEntregador'  ]."</td>";
            echo "<td             >".$Entregas_rs['NomeEntregador']."</td>";
            echo "<td class='vlrc'>".$Entregas_rs['QtdeEntrega'   ]."</td>";
             // Variável $IdEntregadorResumo pra usar no select;
                $IdEntregadorResumo = $Entregas_rs['IdEntregador' ]; 

            echo "<td class='vlrc'>";
                    $sql_EntregasRetorno="select ped.identregador as IdEntregador, count(*) as QtdeFormaRetorno, fpgto.Descricao from pedidoformapgto pedf  
                    inner join pedido ped on pedf.codpedido = ped.codpedido  inner join cdformapgto fpgto on pedf.codforma = fpgto.codforma     
                    where ped.dtmovto = '$DtMov' and ped.status  = 'BT'  and ped.codtpentr = 1  and ped.identregador <> 0 and
                    fpgto.descricao in('RETORNO') and ped.identregador = '$IdEntregadorResumo'
                    group by ped.identregador, fpgto.descricao  order by ped.identregador";

                    $sql_EntregasRetorno = $conn->prepare($sql_EntregasRetorno);
                    $sql_EntregasRetorno->execute();    
                    $rs_EntregasRetorno = $sql_EntregasRetorno->fetchAll(PDO::FETCH_ASSOC);                 

                    foreach($rs_EntregasRetorno as $rr_EntregasRetorno)
                        {
                            echo $QtdeRetorno = $rr_EntregasRetorno['QtdeFormaRetorno'];

                            $TOTQtdeFormaRetorno = 0;   
                            $TOTQtdeFormaRetorno += $rr_EntregasRetorno['QtdeFormaRetorno']; 

            echo "</td>";
                        var_dump($TOTQtdeFormaRetorno); 
                        } //Segundo foreach;    

This foreach receives the $Identregationsummary to be able to display the deliveries that are associated with a particular Form of Payment

Below is the result of the var_dump($Totqtdeflection);

C:\wamp64\www\solus\resumo_dia.php:384:int 4
C:\wamp64\www\solus\resumo_dia.php:384:int 1
C:\wamp64\www\solus\resumo_dia.php:384:int 3
C:\wamp64\www\solus\resumo_dia.php:384:int 2
C:\wamp64\www\solus\resumo_dia.php:384:int 2
C:\wamp64\www\solus\resumo_dia.php:384:int 1
C:\wamp64\www\solus\resumo_dia.php:384:int 1
C:\wamp64\www\solus\resumo_dia.php:384:int 1
//O resultado que eu esperava do var_dump era 15

Below I have this code that I made and this working. The difference between them is that the code below is not inside a foreach.

I have a code that’s working perfectly, which is this: It takes the result of each line and is storing in each variable that , I display at the end, this code is not inside loop.

$sql_EntregasTot= $conn->prepare($sql_EntregasTot);
    $sql_EntregasTot->execute();
    $TOTQtdeEntregas     = 0;
    $TOTVlrTaxas         = 0;
    $TOTQtdeEntregadores = 0;

    $TOTEntregas       = $sql_EntregasTot->fetchAll(PDO::FETCH_ASSOC);
    foreach($TOTEntregas as $TOTEntregas_Resumo)
        {
            $TOTQtdeEntregas     += $TOTEntregas_Resumo['QtdeEntrega'] ;
            $TOTVlrTaxas         += $TOTEntregas_Resumo['VlrTaxa']     ;
            $TOTQtdeEntregadores += 1                                  ;
        }   

Tela

  • Before the first foreach, create a variable that will be the overall total: $somatotal = 0;... and after the second foreach, you sum up with the value of the second foreach: $somatotal += $TOTQtdeFormaRetorno;

  • It would look something like this: https://jsfiddle.net/rb95d8pz/

  • Tries to implement array_sum($Totqtdeformreturn); This function sums all values within an array.

  • When I try to use the array_sum it presents the following error: "Warning: array_sum() expects Parameter 1 to be array, integer Given in C: wamp64 www Solus resume_dia.php on line 390" I am doing so: $SOMA = array_sum($Totqtdeformaretum); var_dump($SOMA);

  • @dvd : With this parameter that you passed me it sum line by line until you reach the final result, note that the final value is 15 what is expected. C: wamp64 www Solus resume_dia.php:391:int 4 C: wamp64 www Solus resume_dia.php:391:int 5 C: wamp64 www Solus resume_dia.php:391:int 8 C: wamp64 www Solus resume_dia.php:391:int 10 C: wamp64 www Solus resume_dia.php:391:int 12 C: wamp64 www Solus resume_dia.php:391:int 13 C: wamp64 www Solus resume_dia.php:391:int 14 C: wamp64 www Solus resume_dia.php:391:int 15

  • So... you can take this amount and play in the column of the final row of the table.

Show 1 more comment

2 answers

0

I don’t quite understand, but wouldn’t it be the case that your variable is setting the zero value at every moment:

$TOTQtdeFormaRetorno = 0;

Should be out of the second foreach, no? So:

$TOTQtdeFormaRetorno = 0;

foreach($rs_EntregasRetorno as $rr_EntregasRetorno)
{
    echo $QtdeRetorno = $rr_EntregasRetorno['QtdeFormaRetorno'];

    $TOTQtdeFormaRetorno += $rr_EntregasRetorno['QtdeFormaRetorno']; 

    echo "</td>";
    var_dump($TOTQtdeFormaRetorno); 
}
  • When I put it away it behaves the same way.

  • @Jeffersonamorim you want to add all values returned from $rs_EntregasRetorno every loop, that’s not it?

  • That’s right, it would be a very simple sum, but it’s not working here. Registration1: 1 Registration2: 5 Registration3: 2 Soma: 8

  • I’ve done this process a few times and they all worked, with a simple while/for, as well as foreach. The only difference is that none of the other times was a loop inside another loop.

0

Problem solved.

I initialized the variables before the main foreach (as I reported within that foreach had other foreach depend on the outcome of that first).

    $TOTQtdeRetornos     = 0;                                          
    $TOTQtdeBonificacao  = 0; 
    foreach($EntregasResumo_rs = $sqlEntregas_res->fetchAll(PDO::FETCH_ASSOC) as $Entregas_rs)
    {

And at the end of that main foreach I made the sum.

    $TOTQtdeRetornos    += $QtdeRetorno;
    $TOTQtdeBonificacao += $QtdeBonificacao;
}

I thank everyone who helped me and helped me reach this solution.

inserir a descrição da imagem aqui

Browser other questions tagged

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