Sum in group table per month

Asked

Viewed 36 times

0

I have the following code below and I would like it to return me the sum of the values separated by the said month of release in PHP, because it is returning the current month.

Example:

DATA         VALOR 
21/01/2018   200.00
23/02/2018   200.00 
25/01/2018   200.00

JANUARY: 400.00 FEBRUARY: 200.00

And so on. Someone who can help me?

<?php

     $conn = mysqli_connect('localhost','root','root','sistema');
     $resultado = mysqli_query($conn, "SELECT sum(total) FROM pedidos GROUP BY YEAR(post_at), MONTH(post_at)  ;" );
     $linhas = mysqli_num_rows($resultado);
     while($linhas = mysqli_fetch_array($resultado)){    
         echo date("m ") . 'R$: ' . $linhas[ 'sum(total)'] .'<br/>';            
?>

<?php
    }
?>

inserir a descrição da imagem aqui

  • Do you want to get the values grouped by month? In the case of 01/01 to 31/01, you want the sum of that month’s records, and so on?

  • Yes, separating them by month. and returning the month of the said sum. In this case my code already groups, but does not return me the month of the sum. me returns the month but not the month of release but the current month. Example: 06 R$: 200 06 R$: 116 06 R$: 100 06 R$: 200

  • The idea would be to create a column always displaying the total sum of the month?

  • The code brings me yes the sum, grouped. but the grouping returns me a month only for all. The month of June. Where should return other months. EXAMPLE: Jun - R$: 200 Jun - R$: 116 Jun- R$: 100 Jun- R$: 200

  • You can put the result to execute SELECT sum(total) FROM pedidos GROUP BY YEAR(post_at), MONTH(post_at) direct on phpmyadmin with a post_at for example ?

  • The problem is in date("m"), this will always return the current date and not the date of the bank’s records.

  • And how to return the date of the records?

  • Solved, thanks to all the comments, I solved with a foreach and picking the variable with a strtotime.

Show 3 more comments
No answers

Browser other questions tagged

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