-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 ;
}
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;
– Sam
It would look something like this: https://jsfiddle.net/rb95d8pz/
– Sam
Tries to implement array_sum($Totqtdeformreturn); This function sums all values within an array.
– Leandro
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);
– Jefferson Amorim
@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
– Jefferson Amorim
So... you can take this amount and play in the column of the final row of the table.
– Sam