Sum total of a loop

Asked

Viewed 73 times

0

I’m trying to make a total sum of a "for" but it’s all too crazy or "zeroed"...

<?php
    for ($i=0; $i < count($data); $i++) {

        $total_receitas_recebidas = 0;
        $total_receitas_a_receber = 0;
        $total_despesas_pagas = 0;
        $total_despesas_a_pagar = 0;

        $sinalNegativo = "";

        if(($data[$i]["tipo"]=="receita")&&($data[$i]["situacao"]=="S")){
            $cor=' style="color:green;"'; 
            $situacao="Receita recebida";
            $total_receitas_recebidas = $total_receitas_recebidas + (float) $data[$i]["valor"];
        }

        if(($data[$i]["tipo"]=="receita")&&($data[$i]["situacao"]=="N")){
            $cor=' style="color:blue;"';
            $situacao="Receita à receber";
            $total_receitas_a_receber = $total_receitas_a_receber + (float) $data[$i]["valor"];
        }

        if(($data[$i]["tipo"]=="despesa")&&($data[$i]["situacao"]=="S")){
            $cor=' style="color:orange;"';
            $situacao="Despesa paga";
            $sinalNegativo = "-";
            $total_despesas_pagas = $total_despesas_pagas + (float) $data[$i]["valor"];
        }

        if(($data[$i]["tipo"]=="despesa")&&($data[$i]["situacao"]=="N")){
            $cor=' style="color:red;"';
            $situacao="Despesa à pagar";
            $sinalNegativo = "-";
            $total_despesas_a_pagar = $total_despesas_a_pagar + (float) $data[$i]["valor"];
        }

        echo '<tr>';
        echo '<td>';echo $data[$i]["info_para_cliente"];  echo '</td>';
        echo '<td>';echo converteDataParaMostrar($data[$i]['data_vencimento']);  
        echo '</td>';
        echo '<td><p'.$cor.'><b>';echo 'R$ '.$sinalNegativo.number_format($data[$i]["valor"], 2, ',', '.'); echo '</b></p></td>';
        echo '</tr>';
    }


echo '<input type="hidden" id="total_receitas_recebidas" value="'.number_format($total_receitas_recebidas, 2, ',', '.').'">';
echo '<input type="hidden" id="total_receitas_a_receber" value="'.number_format($total_receitas_a_receber, 2, ',', '.').'">';
echo '<input type="hidden" id="total_despesas_pagas" value="'.number_format($total_despesas_pagas, 2, ',', '.').'">';
echo '<input type="hidden" id="total_despesas_a_pagar" value="'.number_format($total_despesas_a_pagar, 2, ',', '.').'">';

I did some tests with a value of "150.00" X4 records but it returns "300.00"????

Obs.: this is a simple cash flow, the query searches for revenues and expenses and cross everything.

I have a similar model in another project and it works perfect...

  • Tested 4x with "150", but which tipo and the situacao? Where you get the "300"?

2 answers

2


First you are instantiating the calculation variables inside the for or be each loop it Zera the calculation, put the following variables out of the loop and test again:

$total_receitas_recebidas = 0;
$total_receitas_a_receber = 0;
$total_despesas_pagas = 0;
$total_despesas_a_pagar = 0;

2

You’re zeroing the totalizers at every loop step, and the totalisers are cumulative, so I suggest your code stays that way:

$total_receitas_recebidas = 0;
$total_receitas_a_receber = 0;
$total_despesas_pagas = 0;
$total_despesas_a_pagar = 0;


for ($i=0; $i < count($data); $i++) {

    if(($data[$i]["tipo"]=="receita")&&($data[$i]["situacao"]=="S")){
        [...]
        $total_receitas_recebidas += $total_receitas_recebidas + (float) $data[$i]["valor"];
    }

    if(($data[$i]["tipo"]=="receita")&&($data[$i]["situacao"]=="N")){
       [...]
        $total_receitas_a_receber += $total_receitas_a_receber + (float) $data[$i]["valor"];
    }

    if(($data[$i]["tipo"]=="despesa")&&($data[$i]["situacao"]=="S")){
        [...]
        $total_despesas_pagas += $total_despesas_pagas + (float) $data[$i]["valor"];
    }

    if(($data[$i]["tipo"]=="despesa")&&($data[$i]["situacao"]=="N")){
        [...]
        $total_despesas_a_pagar += $total_despesas_a_pagar + (float) $data[$i]["valor"];
    }

    echo [...]
}

Moreover, it seems to me that its variable $signNegative is not required, since the number_format function preserves the original sign of the numeral.

Browser other questions tagged

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