Order by Asc to list increasing

0

I need to change the order of items in a listing. Which is decreasing and needs to change to growing.

It's a code not made by me, but I need to maintain it.

<div class="wrapper wrapper-noticia-listagem cf" data-rel-evento="noticias" <?php if($noticias_tag) { echo 'style="display: block;"'; } ?> >
  <?php
                                foreach($noticias_tag as $noticia_tag) {
                                    if(trim($noticia_tag['link_direto']) === "" or $noticia_tag['link_direto'] === null) {
                                        $categoria_slug = $noticia_tag['categoria_slug'];
                                        $noticia_slug = $noticia_tag['slug'];
                                        $noticia_id = $noticia_tag['id'];
                                        $link = "/noticias/$categoria_slug/$noticia_slug-$noticia_id";
                                        $target = "";
                                    } else {
                                        $target = "target='_blank'";
                                        $link = $noticia_tag['link_direto'];
                                    }
                            ?>
    <p>
      ​<strong>
                                    <a href="<?php echo $link; ?>" <?php echo $target; ?>>
                                        <?php echo $noticia_tag['titulo']; ?>
                                    </a>
                                </strong><br>
      <?php echo $noticia_tag['chamada']; ?>
      <br> Enviada em
      <?php echo datetimetostrd($noticia_tag['data_publicacao']); ?> - <a href="<?php echo $link; ?>" <?php echo $target; ?>>Leia mais</a>
    </p>
    <?php } ?>
</div>

So I understand, I need to create a variable that will grab the whole div that contains the news listing in p and there order via PHP.

    
asked by anonymous 09.08.2017 / 15:11

2 answers

0

Felipe, I do not know if I understood correctly, you need to order the $ noticia_tag ['title'] correct ?. Then you can sort the array before inserting into the div, PHP has some functions that do this, if it is already in descending order you can use the array_reverse function. in the case:

$revertido = array_reverse($noticia_tag);

Or rewrite with the asort () functions

asort ($ noticia_tag);

If this was not your goal, disregard.

    
09.08.2017 / 16:02
0

Given that you want to sort by date of publication, the best solution is to find the code that creates the $noticias_tag array, whose data probably comes from the database, and there in the query insert a ORDER BY data_publicacao clause. p>

You can do this in the view , code snippet you showed, even if it is somewhat incorrect, would not be the place to treat the data. Before using the news in foreach($noticias_tag as $noticia_tag) , you can:

function regra_comparacao($a, $b){

    // Presumo que o tipo do dado seja DATETIME mesmo,
    // sem necessidade de conversão, senão converta aqui!
    $a = $a['data_publicacao'];
    $b = $b['data_publicacao'];

    if ($a == $b)
        return 0;

    return ($a < $b) ? -1 : 1;
}

usort($noticias_tag, "regra_comparacao");
    
09.08.2017 / 16:11