There are several ways to do this, one of them is to take advantage of the while
and create an accumulator, which with each iteration of the while
add the value of the price.
<?php
include('connect-mysql.php');
$sqlget = "SELECT * FROM contact_form_info";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting');
$totalPreco = 0;
echo "<table>";
echo "<tr> <th>ID</th> <th>Nome</th> <th>Whatsapp</th> <th>Preço</th></tr>";
while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
//Acumulador do preço, totalizador
$totalPreco += $row['preco'];
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['nome'];
echo "</td><td>";
echo $row['whatsapp'];
echo "</td><td>R$";
echo $row['preco'];
echo "</td><td>";
}
echo "</table><br>";
echo "<h1> Total Faturamento: R$ $totalPreco.</h1>";
?>
You can also choose to create another query that returns only the totals, using the sum
:
<?php
include('connect-mysql.php');
$sqlget = "SELECT * FROM contact_form_info";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting');
//Bsuca o total dos preços utilizando o SUM do SQL
$sqlgetTotal = "SELECT SUM(preco) as total_preco FROM contact_form_info";
$sqldataTotal = mysqli_query($dbcon, $sqlgetTotal) or die('error getting');
$rowTotal = mysqli_fetch_array($sqldataTotal, MYSQLI_ASSOC);
$totalPreco = $rowTotal['total_preco'];
echo "<table>";
echo "<tr> <th>ID</th> <th>Nome</th> <th>Whatsapp</th> <th>Preço</th></tr>";
while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
//Acumulador do preço, totalizador
$totalPreco += $row['preco'];
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['nome'];
echo "</td><td>";
echo $row['whatsapp'];
echo "</td><td>R$";
echo $row['preco'];
echo "</td><td>";
}
echo "</table><br>";
echo "<h1> Total Faturamento: R$ $totalPreco.</h1>";
?>
worked perfectly!!! thank you.
– Felipe S.
Great! You’re welcome! D
– Daniel Mendes