List table with increasing numbers with pagination

Asked

Viewed 37 times

0

Hello, I’m doing a donor ranking, but I had a little problem I am wanting to list all users and want to organize by increasing numbers, ex: 1,2,3,4,5,6..

but when I go to the next page the numbers keep

S: I cannot list by ID (ORDER BY id ASC), pos if it is a ranking, the query below is only to explain

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I did something like, but without success

$total_pages = $api->query('SELECT * FROM doadores')->numRows();
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;

$num_results_on_page = 5;

$calc_page = ($page - 1) * $num_results_on_page;
if ($stmt = $api->query('SELECT * FROM doadores LIMIT ?,?', $calc_page, $num_results_on_page)->fetchAll()) {

$num = 0;
foreach ($stmt as $row){ ?>

<tr>
<td><?php echo $num++; ?></td>
<td><?php echo $row['nome']; ?></td>
</tr>

...

PREV 1 2 3 4 ... 120 NEXT

  • It’s not enough to do ($page - 1)*$num_results_on_page + $num++ instead of just $num++?

  • @Woss in general would look the same '-'

  • Why do you say that?

  • @Woss $num = 1; $calc_page = ($page - 1) * $num_results_on_page + $num++; = always returns 2

  • That’s not $calc_page, but yes your echo inside the table

  • worked perfectly, thank you. could answer the question for me mark your answer?

Show 1 more comment

1 answer

0

Hello,

You can also bring straight from the database using the function row_number():

select 
    -- o 'order by' abaixo deve ser o mesmo ... -> 
    row_number() over (order by ranking) sequencia
    , * 
from 
    doadores
    --> deste 'order by'
order by ranking 
limit 5
offset 0

An example: sqlfiddle

Browser other questions tagged

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