Direct sum in PHP

1

I have a service control system. I would like to issue a service report and add these values but I did not want to do with the command in sql I want to make direct in php , type sum values of column valor_trabalho of listed services only

would be like this

OS | DESCRICAO | VALOR

 1 |  visita   | 150,00

 2 |  visita   | 130,00

total = ???

My sql and so

$trabalho=mysql_query( "SELECT * FROM cad_trabalho WHERE id_trabalho = '$id'");'

I use this function to display the data

<?php
while($row = mysql_fetch_object($trabalho)) {                                    
echo "<tr><td>$row->os</td><td>$row->descricao</td><td>$row->valor</td></tr>
      <tr>total = ???</tr>";
}
 ?>
    
asked by anonymous 03.05.2016 / 22:40

1 answer

1

A simple example:

<?php
   $total = 0;
   while($row = mysql_fetch_object($trabalho)) {                                    
      $total += $row->valor;
      echo "<tr><td>$row->os</td><td>$row->descricao</td><td>$row->valor</td></tr>";
   }
   echo "<tr><td colspan="3">TOTAL: $total</td></tr>";
?>

This is clear, assuming that $rou->valor is numeric. If it is string , it depends on the format. It may be necessary to change commas by period, etc.

Just to note, $total += $row->valor is the short way to write $total = $total + $row->valor

    
03.05.2016 / 23:01