Add values of a column

1

people .. I have this table:

What I want is to add all the values in the TOTAL column and be able to show them where it says subtotal .. can anyone help me with the formula I would have to do to show that? then the rest of the items is already calculated with the subtotal ..

pd: the data below are written by hand in html, without any formula.

php code:

    
asked by David Bucci 05.12.2018 в 02:36
source

2 answers

0

Answer: Add a variable $total=0 and when you go consulting the total of the elements go add it. Ex:

$total=0
...
$while($columna=mysqli_fetch_array($resultado)){
     echo "<tr>";
     ...
     echo "</tr>";
     $total+=$columna['total'];
}
...
echo "</table>";
echo "<table>";
echo "<td>SubTotal</td>";
echo "<td>".$total."</td>";
echo "</table>";

Tip: Add if there is a column that meets the query

$count=mysqli_num_rows($resultado);
if($count > 0){
    while($columna=mysqli_fetch_array($resultado)){
         .....
    }
}
    
answered by 05.12.2018 / 03:05
source
0

The formula you ask is the sum and the function in sql is SUM()

For the subtotal, the query would be of this type.

SELECT
    SUM(total) as 'subtotal'
FROM factura1

And it gives something of type.

| subtotal |
|     1190 |

You may want to make a variable of type.

$subtotal = "SELECT
                 SUM(total) as 'subtotal'
             FROM factura1";

I guess what you would have to deal with would be with the type of data that is in the "query" column.

    
answered by 05.12.2018 в 02:58