Add records from a field and display in PHP (values have R$ in front)

Asked

Viewed 325 times

-1

Hello, I’m trying to sum up some values using the SUM and is returning 0.

The fields I will use:

  1. category
  2. month
  3. value (want to add this)

I want to add the valor of all records of each category and display next to the same category.

Code:

  // SELECT NOS CAMPOS A SEREM EXIBIDOS
  $result_transacoes = "SELECT categoria, mes, sum(valor) FROM contas WHERE mes = 'JANEIRO' GROUP BY categoria ORDER BY categoria ASC";
  $resultado_trasacoes = mysqli_query($con, $result_transacoes);
  while($row_transacoes = mysqli_fetch_assoc($resultado_trasacoes)){

    $html .= '<tr><td>'.$row_transacoes['categoria'] . "</td>";
    $html .= '<td>'.$row_transacoes['mes'] . "</td>";
    $html .= '<td>'.$row_transacoes['sum(valor)'] . "</td>";
  }

I started trying to add up the total value of each category, however, it did not work, is returning "0", perhaps because of the R$? What can I do in this case? I tried to use the SUM.

  • Hello @Sam, values are being saved with R$ yes, I removed all the R$ of the column to make a test, but the sum of them did not hit, some were like: 6041.0294810302, and another concern is, how will I put a mask of monetary value on an input without saving the "R$" ?

  • @Sam, I removed the "R$" "." "," and put as INT, apparently I think it worked out the sum, but I’m not able to divide the value by 100, I tried to put in the own select, so: SELECT categoria, mes, sum(valor)/100 FROM contas

  • Man, it’s a simple thing and I’m not getting... I did it like this: $valor_dividido = 'sum(valor) / 100';&#xA;&#xA; $html .= '<td>'.$row_transacoes['$valor_dividido']. "</td>";

  • It would be right: $valor_dividido = $row_transacoes['sum(valor)'] / 100; and here would be: $html .= '<td>'.$valor_dividido. "</td>";

2 answers

0


I suggest working with monetary values in a column type INT and save the values in cents. For example, R$10 (or R$10,00) you save with the value 1000 (thousand cents).

By pulling the column value, you divide by 100 and have the real value, with the cents separated by point (ex.: 1000 will stay 10.00). For example, if the value is 150000 (one hundred and fifty thousand cents) which is equivalent to R$1,500.00 (one thousand and five hundred Reais) you will divide by 100 and use the function money_format() PHP to display the value with the Brazilian Real formatting, including adding the symbol R$. An example:

<?
setlocale(LC_MONETARY,"pt_BR");
$valor = "150000";
echo money_format("%n", $valor/100); // R$ 1.500,00
?>

You have to use the setlocale to set the format. The line setlocale(LC_MONETARY,"pt_BR"); should be defined only once. You can put it in a general site include because it will already be active in the entire site environment, without having to declare again.

  • I don’t understand......

  • Use the normal mask. When vc send the value to PHP, before saving to the bank, vc must convert to cents. For example, if you send 12,00 (twelve real), you choose the comma before putting in the bank.

  • Before placing in the bank you make a replace by removing the comma and the dot. Use str_replace. I mean, 1,200 will turn into 120,000.

  • I tried that way: <script>&#xA; // NAO IR PARA O BANCO&#xA; function formatar($valor){&#xA; $pontos = array(",", ".", "R$");&#xA; $result = str_replace($pontos, "", $valor);&#xA; return $result;&#xA; }&#xA; </script> , but the value is reset to the bank...

  • Man, I couldn’t use the replace until now, when he removes the "." or "," it erases the value that is then... example: 1,500.00 , the bank only saves the 1

-2

All right, Samuel? Come on!

You can resolve this in your query and your code will get cleaner. Just add group by categoria before the order by that already solves.

This code snippet creates a category-level grouping and the sum will automatically be separated by category.

NOTE: Necessary to remove the R$ with the code snippet REPLACE(valor, "R$", "") and then convert the text to decimal. See the query in the link below.

Take the example: http://sqlfiddle.com/#! 9/4766a1/3

  • Hello Tiago, the category part worked, but I believe that the sum of the values did not... it seems that took only the first value of each category. With the sum(valor) values have been reset. Maybe it’s not because of R$?

  • That’s correct @Samuel. I made the correction in the answer.

Browser other questions tagged

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