Loop to sum values of one db

0

I'm doing a financial control system, and I've looped while grabbing the client data from a db and it shows them in a table. What I want to do is take the values in the column "PRICE" and add, to give the value of the total of balances. I'm going to put only the part of the relevant code:

    <?php
                        $seleciona="select * from receitas order by nome";
                        $sql=mysqli_query($con,$seleciona);
                        while ($inf=mysqli_fetch_row($sql)) {
                                $cliente=$inf[0];
                                $data=$inf[1];
                                $horario=$inf[2];
                                $preco=$inf[3];
                                $servico=$inf[4];

                    echo"           
                              <td class='mdl-data-table__cell--non-numeric'>$cliente</td>
                              <td class='mdl-data-table__cell--non-numeric'>$data</td>
                              <td>$horario</td>
                              <td class='mdl-data-table__cell--non-numeric'>$preco</td>
                              <td class='mdl-data-table__cell--non-numeric'>$servico</td>
                            </tr>
                            ";}

                ?>

What I want to do now is a loop to get the values of the variable "$ price" and add up to put the result of the sum of all values as the total revenue.

    
asked by anonymous 29.07.2017 / 21:37

1 answer

0

Correct is $total=$total+$inf[3]; or $total=$total+$preco;

<?php
     $total=0;
     $seleciona="select * from receitas order by nome";
     $sql=mysqli_query($con,$seleciona);
     while ($inf=mysqli_fetch_row($sql)) {
         $cliente=$inf[0];
         $data=$inf[1];
         $horario=$inf[2];
         $preco=$inf[3];

         $total=$total+$inf[3];

         $servico=$inf[4];

           echo"           
              <td class='mdl-data-table__cell--non-numeric'>$cliente</td>
              <td class='mdl-data-table__cell--non-numeric'>$data</td>
              <td>$horario</td>
              <td class='mdl-data-table__cell--non-numeric'>$preco</td>
              <td class='mdl-data-table__cell--non-numeric'>$servico</td>
              </tr>
          ";}

?>
  

When you do $total=$preco+$inf[3]; as stated in the comment, at each iteration of the loop it is adding $preco , which is nothing more than $inf[3] , with $inf[3] being the same values. At the end of the loop the result is double the last value returned from the table.

 $preco=$inf[3];
 $total=$preco+$inf[3];

                 preco    $total=$preco+$inf[3];
 ponteiro ->     20.00    20.00 + 20.00 = 40.00
                 30.00
                 40.00
                 .....

                 preco    $total=$preco+$inf[3];
                 20.00       
 ponteiro ->     30.00    30.00 + 30.00 = 60.00 
                 40.00
                 .....
  

Already with $total=$total+$inf[3];

                 preco    $total=$total+$inf[3];
 ponteiro ->     20.00      0 + 20.00 = 40.00
                 30.00
                 40.00
                 .....

                 preco    $total=$total+$inf[3];
                 20.00       
 ponteiro ->     30.00     40.00 + 30.00 = 70.00 
                 40.00
                 .....

                 preco    $total=$total+$inf[3];
                 20.00       
                 30.00     
 ponteiro ->     40.00     70.00 + 40.00 = 110.00 
                 .....
    
30.07.2017 / 16:50