Format in R$ rescued values with SUM

Asked

Viewed 78 times

1

I want to show the values in currency format

include "conect.php";
$sql = "SELECT SUM(preco) as SOMA, cod FROM valores GROUP BY cod ORDER BY SOMA DESC";
$exec = mysql_query($sql);

while ($rows = mysql_fetch_assoc($exec)) {
     echo $rows["cod"]."-".$rows["SOMA"]."<br><br><br>";

}

Visualization:

BT01-151000

BT02-48000

BT03-22000

BT04-11000

Desired:

BT01- R$ 1.510,00

BT02- R$ 480,00

BT03- R$ 220,00

BT04- R$ 110,00

  • And the php code?

  • <?php include "conect.php"; $sql = "SELECT SUM(price) as SOMA, Cod FROM GROUP BY Cod ORDER BY SOMA DESC"; $exec = mysql_query($sql); while ($Rows = mysql_fetch_assoc($exec)) { echo $Rows["Cod"]." -". $Rows["SOMA"]." <br><br>"; } ?>

1 answer

3


You can do it using the php function number_format:

Ref: http://php.net/manual/en/function.number-format.php

function formatar_valor($valor){
      return number_format($valor, 2, '.', '');
}


include "conect.php";
$sql = "SELECT SUM(preco) as SOMA, cod FROM valores GROUP BY cod ORDER BY SOMA DESC";
$exec = mysql_query($sql);

while ($rows = mysql_fetch_assoc($exec)) {
     echo $rows["cod"]."- R$ ".formatar_valor($rows["SOMA"])."<br><br><br>";
}

Browser other questions tagged

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