Order by Asc to list increasingly

Asked

Viewed 55 times

0

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

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

<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>

From what I understand, I need to create a variable that will take all the div which contains the news listing on p and ali order via PHP.

  • You can use the function usort passing the function that compares elements of the type of the list you have

  • What is the field by which you want to sort? Title, date of publication, author?

  • Maybe making this query sql change would solve your problem easier

2 answers

0

Felipe, I don’t know if I understand correctly, you need to order the $noticia_tag['title'] correct?.
So you can sort the array before inserting it 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 reodernate with asort functions()

asort($noticia_tag);

if that wasn’t your goal, disregard.

  • Actually I’d like to sort out what’s inside the div, the p in the case.

0

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

You can do that in view, code snippet that you showed, although it is somewhat incorrect, would not be there not the place to process the data. Before using the news on foreach($noticias_tag as $noticia_tag), so order:

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");
  • Actually I’d like to sort out what’s inside the div, the p in the case.

Browser other questions tagged

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