大型PHP阵列分页

I have an array that has over 10k results (generated by a MySQL query) and I wonder what is the best practice for paginate it? Extract all the data first then split it to pages or there is another faster/best way?

Use MySQL's LIMIT syntax to only retrieve the desired results from the database before it even reaches your PHP code.

Something like this:

$offset = 0;
$per_page = 25;

$query = "SELECT * FROM `blah` LIMIT $offset, $per_page";

...

As suggested, you can use LIMIT. To find out the total number of results, you can optionally do:

SELECT SQL_CALC_FOUND_ROWS * FROM `table` ... LIMIT 0, 20

Then:

SELECT FOUND_ROWS() AS `total_results`

To retrieve your paginator's maximum page number.

You will probably want to do a COUNT() query before pulling your content to determine how many rows you have in total. The general algortihm for finding out how many pages you need is then

ceil( totalRows / rowsPerPage )

To display the pagination navigation links for this many records, consider using "logarithmic" page navigation. See here:

How to do page navigation for many, many pages? Logarithmic page navigation