How can I paginate these results? [duplicate]

0

I made a SQL to filter results according to the desired parameters that resulted in the following statement:

$query = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND '));

Since I used PDO to mount the $query do I have to do the result pagination in PDO or can I do it in MySQL ?

Example of $query result:

SELECT * FROM imovel WHERE 1=1 AND CATEGORIA IN ('CASA') AND DORMITORIO IN (2)

I also have to execute pagination, the number of pages:

    
asked by anonymous 10.10.2014 / 14:34

1 answer

3

PDO is just a function that will send your SQL statement to the MySQL database.

And the pagination is done in MySQL, or you can also do it via programming ...

Here's a small example:

<?php

   $page = ((isset($_GET['page']) && intval($_GET['page']) > 1)? intval($_GET['page']) : 1) - 1;

   $limite = 15; // limite de registros por página

   $atual = $page*$limite; // Pula a quantidade de registros até os da página atual

   $limit = " LIMIT {$atual}, {$limite}";

   // Registros limitados
   $query = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND ').$limit); 

   // Todos os Registros para criar as página
   $total = $pdo->query("SELECT count(id) as total FROM imovel ".trim($where, ' AND ')); 

   $qtdtotal = $total['total']; // Pegue a quantidade total
   $qtdpage = ceil($qtdtotal/$limite); // Quantidade de páginas

  // Criando os links
  for ($i = 1; $i<$qtdpage; $i++){

     echo '<a href="paginacao.php?page='.$i.'">'.$i.'</a><br>';

  }
So for programming purposes, in this case, it does not make much difference whether you're using PDO , mysql_* or mysqli::* .

Some links about pagination:

  

MySQL Registrations
Understanding the Paging of Records in MySQL
How to limit the amount of pages shown on a page?

    
10.10.2014 / 14:50