How can I paginate these results?

Asked

Viewed 1,021 times

0

I made an SQL to filter results according to the desired parameters which resulted in the following statement:

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

Whereas I used PDO to mount the $query I have to paginate results in PDO or I can do it in MySQL?

Example of the result of $query:

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

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

inserir a descrição da imagem aqui

  • 2

    I recommend not mounting the querys the way you are doing. Search for Preparedstatements. As for the payment, I did not understand very well your question. What exactly you have tried?

  • To make a pagination, use the Mysql LIMIT option, it has two arguments. Do a search...

1 answer

3


Roughly, PDO is just a function that will send your instruction SQL to the database Mysql.

And paging is done in Mysql, or you can also do it via programming...

Here’s a little 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, there’s not much difference if you’re using PDO, mysql_* or mysqli::*.

Some links on paging:

Mysql Pagination of Records
Understanding Mysql Log Paging
How to limit the number of pages shown in a pagination?

  • How will I keep the SQL searched with type variables $_POST?

  • Limiting the number of records consulted to 12 and having all variables in $_POST i how to get the next results.

  • At the end of the listing you place the links of the other pages of the pagination, you can make a query of the amount of all records and make a calculation by dividing by the amount to be displayed on each page, so you will have the number of pages generated...

  • 1

    Try it there, and if you’re not getting your code out so we can help you.

  • It is that the code that you myself rightly limits the amount of records to be searched so I can’t get the total records to use for paging.

  • 1

    That, hence in case it is just you make another query, without the LIMIT

  • Added example...

  • Fatal error: Cannot use Object of type Pdostatement as array in /home/axitech/www/imo/wp-content/themes/colnaghi/modulos/resultado_busca.php on line 47 ($qtdtotal = $total['total'];)

  • The code I put there is just for example, you have to adapt it to your reality, you have a column called id? If not, put the key field of your table, or just *, thus count(*) as total. You have to receive this data the same way you receive the others.

Show 4 more comments

Browser other questions tagged

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