How does pagination done with PHP + AJAX?

5

I was browsing through the site looking for a functional example of pagination with AJAX + PHP, when I found this example

Sorry for my ignorance, but the 'asynchronous loading' of AJAX causes me doubts ..

However, I had a question: In the script, there is a limit made by AJAX of 6 records to be displayed per page. Above all, there is no limit if I make a SELECT * FROM tabela . So it was assumed that if there are 500 records in the table, they will all be loaded but only displayed six per page? Would not this slow down the flow of the site? I say the loading time?

Another question: How can I change the array in the example by a SELECT , so I can use a mysqli_fetch_array to fetch each row of the record?

Note: If necessary, I'll be posting the code (which is somewhat extensive) for better visualization.

    
asked by anonymous 20.01.2016 / 15:52

2 answers

3

In this example, AJAX actually displays records 6 in 6, but all are loaded. If there were a thousand records, for example, it would be a problem.

A simple alternative is to prepare a php page containing a limited SELECT to X records. You must pass by parameter the page number that will be displayed and the page will return the records.

The SQL command can be this way (php):

$mysqli->query('SELECT * FROM tabela ORDER BY id DESC LIMIT '.$inicio.','.$limite.'');

The return of this search will be the records in the given range. To learn how it works: link .

You should return this query as an array for AJAX. You can understand how to do this here: link .

But for pagination to work, you must compute the $ start variable.

To calculate this start, you can do this:

$limite = 3; // Limite de registros por paginação
$pagina = (isset($_GET['page']))? $_GET['page'] : 1; // Recebe o número da paginação por método GET
$inicio = ($limite * $pagina) - $limite; // Cálculo do início

You can also get the total number of pages:

$result = $mysqli->query("SELECT * FROM tabela");
$total = $result->num_rows; // Quantidade total de registros
$numPaginas = ceil($total/$limite); // Quantidade total de páginas
    
20.01.2016 / 16:26
1

In pagination, the SELECT used in the database must use LIMIT to tell you how many records to fetch and OFFSET to tell you how many records to skip. In the link you posted does not tell you how SELECT is done, it just mounts a Javascript array to use as a sample.

  

Another question: How can I change the array in the example by a SELECT, so I can use a mysqli_fetch_array to fetch each record line?

Your ajax request will call a PHP script, this script will in turn fetch the data in the database using the method you want, be it PDO, mysql, mysqli, etc.

For PHP to know how many records to jump in the SELECT, you need to enter the page number that the user wants to see, this parameter will be sent by AJAX to the PHP script.

Example displaying 10 records per page
The URL of the ajax will be "script.php? Pagina = 1", "script.php? Pagina = 2", "script.php? Pagina = 3", etc ...

// No script.php:
$registrosPorPagina = 10;
$pagina             = intval($_GET['pagina']);

// Calcula o offset de acordo com o número da página
$offset             = $pagina == 1 ? 0 : ($pagina * $registrosPorPagina) - $registrosPorPagina;

// Comando SELECT dinâmico, sempre irá buscar só 10 registros de acordo com a página
// offset 0: registros 1-10
// offset 1: registros 11-20
// offset 2: registros 21-30
$sql = "SELECT * FROM tabela LIMIT 10 OFFSET {$offset}";
    
20.01.2016 / 16:06