Selects post sorted by id

Asked

Viewed 30 times

1

opa, Ai friends... well, the problem I’m having, I made a query to the database where I get all posts ordered by descending id, and just doesn’t work... it selects only the first.

follows the code

<?php
    include 'connect.php';
$title = null;
$text = null;
    $select = mysqli_query($con, "SELECT * FROM posts ORDER BY idp DESC");
    // $conta = mysqli_num_rows($select);
        if (mysqli_num_rows($select) <= 0) {
            echo "<script>alert('Nao ha posts')</script>";
        }else{
            while ($in = mysqli_fetch_array($select)) {
                $title = $in['titulo'];
                $text = $in['texto'];
                $id = $in['idp'];
            }
        }
?>
<div class="wrap-p">
    <div id="title"><?php echo $title;?></div>
    <div id="texto"><?php echo $text;?></div>
    <div id="texto"><?php echo "$id";?></div>
</div>
</div><br><br>

<div class="wrap-p">
    <div id="title"><?php echo $title;?></div>
    <div id="texto"><?php echo $text;?></div>
    <div id="texto"><?php echo "$id";?></div>
</div>
</div><br><br>
  • It seems your variables don’t list. Transform them into an array and add as the query brings a result, then do a for to render it in HTML.

  • Hello @leandro, Welcome to Sopt, before you start make a visit to our [Tour].

1 answer

1

It seems to me that your problem is that your HTML is out of the while, so he’s just showing off the last item of his interaction.

<?php
    include 'connect.php';
    $title = null;
    $text = null;
    $select = mysqli_query($con, "SELECT * FROM posts ORDER BY idp DESC");
    // $conta = mysqli_num_rows($select);
        if (mysqli_num_rows($select) <= 0) {
            echo "<script>alert('Nao ha posts')</script>";
        }else{
            while ($in = mysqli_fetch_array($select)) {
                $title = $in['titulo'];
                $text = $in['texto'];
                $id = $in['idp'];

         # ; toda vez que o while passar aki ele vai adicionar esse trecho
         # ; de codigo HTML na pagina, ou seja, para cada registro retornado
         # ; ele vai adicionar o $title, $text, e $id
?>

<div class="wrap-p">
    <div id="title"><?php echo $title;?></div>
    <div id="texto"><?php echo $text;?></div>
    <div id="texto"><?php echo "$id";?></div>
</div>
<br><br>

<?php

            } # ; <- fim do while
        }     # ; <- fim do else
?>

In the above code I am opening and closing the php tag several times, in which case you can also make use of the format heredoc <<< can create an output string, see the example:

<?php
    include 'connect.php';

    $select = mysqli_query($con, "SELECT * FROM posts ORDER BY idp DESC");
    // $conta = mysqli_num_rows($select);
        if (mysqli_num_rows($select) <= 0) {
            echo "<script>alert('Nao ha posts')</script>";
        }else{
            while ($in = mysqli_fetch_array($select)) {

                # ; inicio da string, abri ela com o texto HTML
                echo <<<HTML

<div class="wrap-p">
    <div id="title">{$in['titulo']}</div>
    <div id="texto">{$in['texto']}</div>
    <div id="texto">{$in['idp']}</div>
</div>
<br><br>

HTML;
# ; ^ fim da string fechar ela com o 
# ; mesmo texto da abertura no caso HTML
# ; [NOTA] não pode haver nada alem do `HTML;` nesta
# ; linha, no teste no ideone até um ` ` (espaço) 
# ; apos o `;` gerou erro

            } # ; <- fim do while
        }     # ; <- fim do else

?>

Online example

  • I tried to put this... nothing appears on the screen

  • I updated the first code by putting the <?php instead of short tag <?, and in the second code I took the comment on the line where is the end of the heredoc, added an online example.

Browser other questions tagged

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