How to Paginate using PHP and MYSQL

Asked

Viewed 350 times

0

I have a simple query that brings the results and assembles a table and I wanted after the table to come a pagination: (), but I don’t know how to do.

The consultation is like this:

$nome=$_GET['nome'];
if($nome!=''){
    $filtronome = " WHERE nome LIKE '%{$nome}%' ";
}else{
    $filtronome = "";
}
$query = ("SELECT * FROM lancamentos ".$filtronome);
$db -> setQuery($query);
$results = $db -> loadObjectList();

echo '<table>
        <thead>
            <tr>
            <td>ID</td>
            <td>Nome</td>
            </tr>
        </thead>
        <tbody>';
foreach($results as $r){
    echo '<tr>';
    echo '<td>'.$r->id.'</td>';
    echo '<td>'.$r->nome.'</td>';
    echo '</tr>'
}
echo '</tbody></table>';
  • To pay just work with offset and limit Exp: pagina 1 - (01-10): offset=0, limit=10; pagina 2 - (11-20) offset=10, limit=10;

  • In addition, you can also create a logic where you first check how many elements there are in the table using count(*) and split with the amount of elements you want on each page to see how many pages there will be

  • Leandro, I put 3 links as reference, but if you search for "pagination" on the site you will find several other examples;

  • show, I’ll give a read, thanks!

No answers

Browser other questions tagged

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