sum foreach inside foreach

0

I have a question about why the sum inside the foreach is not working.

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

But I would like to know if I'm doing something wrong, or if you can do it otherwise using that same structure.

The situation is this:

The main foreach fetches all deliverers that exist in the request table.

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

I would like to know why the code below is not working, since when performing a var_dump ($ TOTQtForFormat) instead of displaying TOTAL 15, it displays the content values that 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 $ IdSummerSummer to be able to display the deliverers that are associated with a particular Payment Method

Below is the result of var_dump ($ TOTQType);

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 it is working. The difference between them is that the code below is not inside a foreach.

I have a code that is working perfectly, which is this: It takes the result of each line and is storing in each variable that, at the end, this code is not inside a 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                                  ;
        }   

    
asked by anonymous 01.07.2018 / 20:33

2 answers

0

I do not quite understand, but it would not be the case that your variable is always setting zero at

$TOTQtdeFormaRetorno = 0;

Should be out of the second foreach, should not it? So:

$TOTQtdeFormaRetorno = 0;

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

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

    echo "</td>";
    var_dump($TOTQtdeFormaRetorno); 
}
    
01.07.2018 / 21:23
0

Problem solved.

Initialize the variables before the line of the main foreach (because as I reported inside that foreach had other foreach depend on the result of that first).

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

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

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

Thanks to all who helped me and helped me to get this solution.

    
02.07.2018 / 02:11