How do PHP read Mysql from the bottom up?

Asked

Viewed 191 times

0

My code is this:

    //Receber o número da página
    $pagina_atual = filter_input(INPUT_GET,'pagina', FILTER_SANITIZE_NUMBER_INT);       
    $pagina = (!empty($pagina_atual)) ? $pagina_atual : 1;

    //Setar a quantidade de itens por pagina
    $qnt_result_pg = 8;

    //calcular o inicio visualização
    $inicio = ($qnt_result_pg * $pagina) - $qnt_result_pg;

    $result_usuarios = "SELECT * FROM posts LIMIT $inicio, $qnt_result_pg";
    $resultado_usuarios = mysqli_query($conn, $result_usuarios);
    while($row_usuario = mysqli_fetch_assoc($resultado_usuarios)){

        echo "<div class='post'>";
        echo "<div class='wrap-ut pull-left'>";
        echo "<div class='userinfo pull-left'>";

        echo "<div class='avatar'>";
        echo "<img src='styles/avatare.jpg' alt=''>";
        echo "<div class='status yellow'> &nbsp; </div>";
        echo "</div>";

        echo "<div class='icons'>";
        echo "<a href='" . utf8_encode($row_usuario['downurl']) . "'><img title='Clique para baixar!' src='styles/icon2.png' alt=''></a><img height='22' title='Verificado!' src='styles/icon1.png' alt=''>";
        echo "</div>";
        echo "</div>";

        echo "<div class='posttext pull-left'>";
        echo "<h2><strong>" . utf8_encode($row_usuario['title']) . "</strong></h2>";
        echo "<hr>";

        echo "<iframe width='500' height='250' src='https://youtube.com/embed/" . utf8_encode($row_usuario['yturl']) . "' frameborder='0' allowfullscreen></iframe>";
        echo "<br /> <br />";
        echo "<strong>➜ Descrição:</strong> <font color='black'>" . utf8_encode($row_usuario['description']) . "</font>";

        echo "<hr>";

        echo "Data: " . utf8_encode($row_usuario['date']);

        echo "</div>";
        echo "<div class='clearfix'></div>";
        echo "</div>";
        echo "<div class='clearfix'></div>";
        echo "</div>";
    }

    //Paginção - Somar a quantidade de usuários
    $result_pg = "SELECT COUNT(id) AS num_result FROM posts";
    $resultado_pg = mysqli_query($conn, $result_pg);
    $row_pg = mysqli_fetch_assoc($resultado_pg);
    //echo $row_pg['num_result'];
    //Quantidade de pagina 
    $quantidade_pg = ceil($row_pg['num_result'] / $qnt_result_pg);

    //Limitar os link antes depois
    $max_links = 2;
    echo "<a href='index.php?pagina=1'>Início</a> ";

    for($pag_ant = $pagina - $max_links; $pag_ant <= $pagina - 1; $pag_ant++){
        if($pag_ant >= 1){
            echo "<a href='index.php?pagina=$pag_ant'>$pag_ant</a> ";
        }
    }

    echo "$pagina ";

    for($pag_dep = $pagina + 1; $pag_dep <= $pagina + $max_links; $pag_dep++){
        if($pag_dep <= $quantidade_pg){
            echo "<a href='index.php?pagina=$pag_dep'>$pag_dep</a> ";
        }
    }

    echo "<a href='index.php?pagina=$quantidade_pg'>Fim</a>";

    ?>

This code is reading what is in the database and playing to the Php page, but it reads the first post for the last, I need it to read from the last post for the first

In case when I publish something in Mysql it will appear on my Site, but it appears the oldest post, and the newest it plays down there.

I need the younger ones to come up, and the old ones to go down.

Can you help? Thank you!!!

1 answer

1


Change the line:

$result_usuarios = "SELECT * FROM posts LIMIT $inicio, $qnt_result_pg";

for

$result_usuarios = "SELECT * FROM posts LIMIT $inicio, $qnt_result_pg ORDER BY 1 DESC";
  • It didn’t work, when I put "ORDER BY 1 DESC" it doesn’t show anything else. I think it’s because of "LIMIT"

  • 1

    @Kleyner puts in place of 1 the name of the column that defines the order of your bank, if it is not the first. For example ORDER BY id DESC. It may also be that your problem is on the PHP side, test the direct query in DB without variables before. Similarly, you can use a ORDER BY id without the DESC if you want from small to large

  • @Bacco I’m a beginner and I don’t understand almost anything, but I think you meant to tell me to try it like this > " $result_usuarios = "SELECT * FROM posts LIMIT $inicio, $qnt_result_pg ORDER BY id DESC"; " (without the other quotes) if that’s it, it didn’t work, nothing appeared, and without DESC he will stay in ASC and will not be the last post for first, will be the first to last.

  • Your LIMIT is in the wrong place, it probably caused an error but you didn’t display it on the screen. Always test directly in the database before testing in PHP, and remember to handle the error return. And when in doubt, have the manual for you to see the order of things, see that the ORDER is before the LIMIT: https://dev.mysql.com/doc/refman/5.7/en/select.html - Remember to take a look, it is normal not to decorate everything. Even with experience, programming depends on a lot of information, so in doubt always consult the documentation of things;.

Browser other questions tagged

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