Group content by month

Asked

Viewed 436 times

0

Good afternoon guys, I need to group a list by month, I use the date field in the table. I tried to do, but he repeats the month to each had of 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 looks. If anyone can help.

I’d like to keep it that way:

  • Ok. We have the current result. But which result you want?

  • That I group by month. Example: <br> Feveiro Noticia 01 Data: 01/02/2016 Noticia 02 Date: 02/02/2016 Noticia 03 Date: 03/03/2016

  • One option would be for you to put the Month title before the loop. The other option would be to create a variable before the loop called $mes_current, from there inside the loop you make a check if $mes_current is different from $reg['mes'], if different, then print $reg['mes'], if it is the same it will not repeat. It’s easy, but if you still have doubts, tell me I ride for you

  • something else, already taking advantage to palpitate in the code, semantically <thead> is an element to be used inside a <table>

  • Got it, I’ll try it here. About <thead> the <table> tag is inside the html. (running for a view). Then I run a load. $html_noticias= new Html(); $html = $html_noticias->load('view/noticias/show.html'); $tmes = '; $tbody = '';

2 answers

2


I left commented 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;
  • Exactly that guy. Thank you very much.

0

First make a foreach in the bank query and save 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 . '");
  • I’ll test it. Thank you.

Browser other questions tagged

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