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?
– Inkeliz
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
– Eduehi
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
– GilCarvalhoDev
something else, already taking advantage to palpitate in the code, semantically <thead> is an element to be used inside a <table>
– GilCarvalhoDev
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 = '';
– Eduehi