Paging in SQL

2

I'm having a question here, not for code, but for what logic.

I have a table with 6000 records in MySQL. I have a PHP script that returns 50 records per page, using the SQL "LIMIT" function.

Now I need to fetch a single record from this table and know which 50-page "page" it will be.

For example, if it is record 68, I need to know that it is on the 2nd page. If it's 120, the 3rd, and so on.

Is this possible with PHP or MySQL? Could someone give me a light?

Thank you.

    
asked by anonymous 04.04.2016 / 13:50

1 answer

0

Well if you're just wanting to find out the page (assuming you already know what position the user / player is in), you can use a simple mathematical function in PHP itself.

For example, if you are showing 50 results per page, just make the position of the user / player divided by 50 and in case you round the decimal value up. Example:

//posição do usuário
$pos = 68;
// isso ira gerar um decimal 1.36
$pag = $pos/50;

// o "ceil" irá arredondar para cima o decimal, no caso de 1.36 para 2
echo "Página: ".ceil($pag);

To know which page will appear another position, just change the value in "$ pos". Another example:

//posição do usuário - Outra posição
$pos = 120;
// isso ira gerar um decimal 1.36
$pag = $pos/50;

// o "ceil" irá arredondar para cima o decimal, no caso de 2.4 para 3
echo "Página: ".ceil($pag);

I hope I have helped, I believe this will solve. But the same can also be done by SQL using the ROUND function.

Att;

    
05.04.2016 / 18:56