Loop to Add values from a db

Asked

Viewed 488 times

1

I’m making a financial control system, and I looped while to take the client data from a db and show them on a table. What I want to do is take the values of the column "PRICE" and add, to give the value of the total balances. I will 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 take the values of the variable "$price" and add to put the result of the sum of all values as the total of revenues.

  • And what kind of data you have in the price column?

  • That’s what I thought at first, I did exactly like this: $total=$price+$inf[3]; but the problem is that it doesn’t add up in a loop, but only the first two values

1 answer

1

The 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 commentary, with each iteration of the loop it is adding up $preco, that is nothing more than $inf[3], with $inf[3] which are the same values. At the end of the loop the result is twice 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 
                 .....
  • Man, thank you so much! It’s amazing how a tiny detail that goes unnoticed in logic affects the whole rest of the system. Vlw!

Browser other questions tagged

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