The posts are getting on the last page in the PHP MYSQLI pagination system

Asked

Viewed 36 times

0

Good evening. Can anyone help me with a problem I’m having? Well... I did a pagination posting system, but when I add more data into the database the new data is on the last page instead of the first page, someone knows how to reverse it?

<?php if (!isset($_GET['busca'])) {
    header("Location: index.php");
    exit;
  }
 

  $busca = mysqli_real_escape_string($conecta, $_GET['busca']);

  $sql = "SELECT * FROM postagem WHERE titulo LIKE '%".$busca."%' OR original LIKE '%".$busca."%' LIMIT $inicio, $total";
  $todos = mysqli_query($conecta, "SELECT * FROM postagem WHERE titulo LIKE '%".$busca."%' OR original LIKE '%".$busca."%'");

  $tr = mysqli_num_rows($todos);
  $tp = $tr / $total;


$query = mysqli_query($conecta, $sql);
while ($resultado = mysqli_fetch_assoc($query)) {
  $titulo = $resultado['titulo'];
  $episodio = $resultado['episodio'];
  $tituloo = $resultado['original'];
  $img = $resultado['img'];
  $id = $resultado['id'];
  
  echo "<div class='bg-cont'><a href='post.php?conteudo=$id'><img src='$img' />$titulo<p><span>$episodio</span></a></div>";

}
    	echo "<div class='limpar'></div>";
    	echo "<div class='paginas'>";
$anterior = $pc -1;
    $proximo = $pc +1;
    if ($pc>1) {
      echo "<a href='?busca=$busca&pg=$anterior'><- Anterior</a>";
    }
    echo "|";
    if ($pc<$tp) {
      echo "<a href='?busca=$busca&pg=$proximo'>Próxima -></a>";
    }
echo "</div>";

?>

  • Have id Auto-increment? ORDER by id DESC in select

1 answer

1


........$busca."%' ORDER BY id DESC LIMIT $inicio, $total";

If you want the lines to return in a specific order, include an ORDER BY clause that indicates how to sort the results.

ORDER BY provides great flexibility for sorting result sets. It has the following characteristics:

  • You can mention one or more columns, separated by commas, to use in sorting.
  • By default, ORDER BY sorts the values in ascending order (from smallest to largest). Any sort column can be followed by ASC if you want to specify an ascending order explicitly. These ORDER BY clauses are equivalent:

    ORDER BY sobrenome, nome
    
    ORDER BY sobrenome, nome ASC
    

    To sort values in descending order (from highest to lowest), put DESC after the sorting column name.

    ORDER BY sobrenome, nome DESC
    

    When you mention a column followed by ASC or DESC, the direction specifier applies to that column. It does not affect the ordering direction of any other column listed in the ORDER BY clause.

  • ORDER BY usually refers to table columns by name:

    SELECT sobrenome, nome FROM t ORDER BY sobrenome, nome
    

    However, it is possible to refer to columns in other ways. If a column has one by the way, you can refer to it through it.

    SELECT sobrenome AS ultimo, nome AS primeiro FROM t ORDER BY ultimo, primeiro
    

    You can also specify a number corresponding to the column position in the list of columns to be displayed (1 for the first, 2 for the second and so on):

    SELECT surname, name FROM t ORDER BY 1,2

Browser other questions tagged

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