How to show the sum of the values of a Column using PHP and SQL?

Asked

Viewed 42 times

0

I want to show the sum of the values that are in the COLUMN preco table contact_form_info in the part of the code where it is written echo "<h1>&nbsp; Total Faturamento: R$$row['sum(preco)'].</h1>";

<?php

include('connect-mysql.php');

$sqlget = "SELECT * FROM contact_form_info";
$sqldata =  mysqli_query($dbcon, $sqlget) or die('error getting');

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)) {
        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>&nbsp; Total Faturamento: R$$row['sum(preco)'].</h1>";

?>

1 answer

0


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>&nbsp; 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>&nbsp; Total Faturamento: R$ $totalPreco.</h1>";

?>
  • 1

    worked perfectly!!! thank you.

  • Great! You’re welcome! D

Browser other questions tagged

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