The publications are getting on the last page in the PHP MYSQLI

0

Good evening. Does anyone help me in a problem I'm having? Well ... I did a paging system, but when I add more data in the database, the new data stays on the last page instead of the first one, does anyone know how to revert 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>";

?>
    
asked by anonymous 19.04.2017 / 03:26

1 answer

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

If you want the rows 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 features:

  • You can mention one or more columns, separated by commas, to use for sorting.
  • By default, ORDER BY sorts the values in ascending order (from lowest to highest). 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 sort 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 sort order 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, you can refer to columns in other ways. If a column has an alias, 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

19.04.2017 / 03:51