开发者

Large PHP Arrays pagination

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.


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


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 )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜