Group content per month

0

Good afternoon, I need to group a list by month, I use the field of type date in the table. I tried to do it, but he repeats the month each had on the list. See my code.

$dados = Connection::select("SELECT *,date_format('data','%d/%m/%y') as 'data_formatada', date_format('data','%M') as mes FROM 'noticias' where mes =' . $mes . '");
                   
                    
                   
                    $tbody = '';
                    
                    foreach ($dados as $reg) {

                        $tbody .= 
                                '
                                <thead>' . $reg['mes'] . '</thead>
                                <div class="medium-3 columns">
                                    <img src="' . $reg['imagem'] . '" width="155px" class="thumbnail" title="' . $reg['titulo'] . '"></img>
                                </div>
                                <div class="medium-9 columns">
                                    <a style="color: #008000;" href="' . URL . 'noticias/exibir-id/' . $reg['id'] . '">' . $reg['titulo'] . '</a><br>
                                    <small class="subheader">Por:<b> ' . $reg['por'] . '</b><br>Data: ' . $reg['data_formatada'] . '</small><br>
                                    <small class="subheader">' . $reg['titulo'] . '</small>
                                </div><hr>
                                    ';
                    }

                    $html = str_replace('#TBODY#', $tbody, $html);
                   

                    

                    return $html;
                    break;

See the image below as it stands. If anyone can help.

Iwouldlikeittolooklikethis:

    
asked by anonymous 04.02.2016 / 16:37

2 answers

2

I mentioned where I made the changes.

Follows:

$dados = Connection::select("SELECT *,date_format('data','%d/%m/%y') as 'data_formatada', date_format('data','%M') as mes FROM 'noticias' where mes =' . $mes . '");



                $tbody = '';

                $mes_atual = ''; // <======== ADICIONADO

                foreach ($dados as $reg) {

                    // ==== ADICIONADO ==========
                    if ( $mes_atual != $reg['mes'] ) {
                        $tbody .= '<thead>' . $reg['mes'] . '</thead>';
                        $mes_atual = $reg['mes'];
                    }
                    // ==========================


                    // Do $tbody foi removido o <thead>
                    $tbody .= 
                            '
                            <div class="medium-3 columns">
                                <img src="' . $reg['imagem'] . '" width="155px" class="thumbnail" title="' . $reg['titulo'] . '"></img>
                            </div>
                            <div class="medium-9 columns">
                                <a style="color: #008000;" href="' . URL . 'noticias/exibir-id/' . $reg['id'] . '">' . $reg['titulo'] . '</a><br>
                                <small class="subheader">Por:<b> ' . $reg['por'] . '</b><br>Data: ' . $reg['data_formatada'] . '</small><br>
                                <small class="subheader">' . $reg['titulo'] . '</small>
                            </div><hr>
                                ';
                }

                $html = str_replace('#TBODY#', $tbody, $html);




                return $html;
                break;
    
04.02.2016 / 19:10
0

First make a foreach in the database query and store it in an array:

foreach ($dados as $reg) {
    $arrayDoBanco = $reg['mes'][] = $reg;
}
var_dump($arrayDoBanco);

Then make a foreach to display the data:

foreach ($arrayDoBanco as $mes => $reg) {
  echo "<div>".$mes."</div>";
  //...
}

You can also simplify your select by using the month function for the datetime or timestamp columns:

("SELECT *,month('data') as mes FROM 'noticias' where mes =' . $mes . '");
    
04.02.2016 / 19:56