How to Do Ascending and Descending Listing in PHP Records

Asked

Viewed 9,332 times

4

My table is as followsimagem do painel de adminstracao

Through PHP, I am listing the MYSQL database records within a WHILE LOOP.
I would like to know how to do so that when clicking on ID, or DATA, the data is displayed in an increasing or decreasing way.

PHP SCRIPT:

<?php                    
$strSQL = "SELECT * FROM TABELA ORDER BY id DESC";
$rs = mysqli_query($conecta,$strSQL); 
$error = mysqli_error($conecta);
if(!$error){
while($row = mysqli_fetch_array($rs)):
?>
<ul class="lista-posts">       
   <li class="seleciona"><input type="checkbox" id="checkbox" name="deletar[]" value="<?php echo $row['id']; ?> " /></li>
    <li class="id"><?php echo $row['id']; ?></li>
    <li class="titulo"><?php echo '<a title="'.$row['titulo'].'" class="link-texto" href="artigo-'.$row['slug'].'">'.substr($row['titulo'], 0, 63).'</a>'; ?></li>
    <li class="data"><?php echo '<data class="data">'.date('d/m/Y', strtotime($row['data'])).'</data>'; ?></li>
    <li class="data"><?php     
    if($row['id_categoria'] == 'faq') {
        $novoIdCategoria = str_replace("faq","duvidas-frequentes",$row['id_categoria']);
        echo '<a href="'.$novoIdCategoria.'.php">'.ucfirst("faq").'</a></data>'; 
    } else {
        echo '<a href="'.$row['id_categoria'].'.php">'.ucfirst($row['id_categoria']).'</a></data>'; 
    } ?></li>        
    <li class="gerenciar"><?php 
        echo '<a title="Ver" class="apagar" href="artigo-'.$row['slug'].'"><label class="icon-eye"></label></a>';
        echo '<a title="Editar" class="editar" href="edita.php?id='.$row['id'].'"><label class=" icon-pencil"></label></a>';?>         
    </li> 
</ul>    
<?php endwhile; ?>

From now on, thank you!

  • There are several ways to do this, one would use some grid js to sort the items another is to do a combo or the same link, when clicked it would make a request to php passed two arguments, by which column should be ordered and the orientation (increasing or decreasing)by being done with ajax or normal php request.

  • I was confused, as I said below. My default query performs the sort of records decreasing through the ID. How should I make each link trigger a different query?

1 answer

4


You will need to run your query again at the click by changing the field and the example sorting orientation:

$strSQL = "SELECT * FROM TABELA ORDER BY id DESC";

$strSQL = "SELECT * FROM TABELA ORDER BY id ASC";

Other field:

$strSQL = "SELECT * FROM TABELA ORDER BY outrocampo DESC";

Upgrading with a more complete way of doing:

On the left side of the link puts an arrow up and on the right side of the link puts an arrow down.

ARROWS WILL BE A LINK POINTING TO THE SAME PAGE, BUT WITH PARAMETERS YOU WILL USE IN SELECT.

<a href="mesma_pagina.php?ordem=crescente&campo=data"> <img src="setapracima.png" /> </a>
 Data
<a href="mesma_pagina.php?ordem=decrescente&campo=data"> <img src="setaprabaixo.png" /> </a>

When you click up, you will put in ascending order your Select IE, ASC then. It’ll stay that way:

   if($_GET['ordem] == "crescente")
      $sql = 'select * from tabela order by '.$_GET['campo'].' asc'; # na verdade nem precisa por asc
   elseif($_GET['ordem'] == "decrescente")
      $sql = 'select * from tabela order by '.$_GET['campo'].' desc'; # precisa por desc
   else
      $sql = 'select * from tabela'; # vai entrar aqui da primeira vez que carregar a página.

$exe = mysql_query($sql);
  • 1

    And would that be done with conditions? If so, how should I pass them? I admit I was confused.

  • @Thiagobarros Depending on how well you know php, you could pass this by $_GET or post and do this treatment when you assemble the query.

  • And what would each link look like? up because the query is executed on the same page, so I don’t know the logic for this..

  • the logic is that Voce will update the page to each link. It is interesting to study a little php before doing this follow link to http://codigofonte.uol.com.br/codigos/comunicaca-entre-paginas-usando-o-metodo-get

  • It is interesting to explain the downvote because it answers the question.

  • 1

    I’m sorry, but I didn’t apply the downvote. And to tell you the truth, I also don’t understand why the person applied it. I understand that your answer is correct, and I understood the query, the problem is in my knowledge not to apply correctly to my script.

  • If it helped you mark it as solved and ask a question of how to use GET in php.

  • In my view, it would be correct to mark as solved if I had actually solved the problem, until I could edit the post with the answer. But if in your vision is already solved, and you being experienced here on the site, I will respect your opinion.

  • @Thiagobarros updated with a more complete answer

  • Thank you Otto! Now I think I can adapt it to my needs.

Show 6 more comments

Browser other questions tagged

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