Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to paginate results in php

Tags:

php

pagination

I need to display many pages of news in a site. Should I do the pagination in the database query using LIMIT or with the PHP script after getting all the results?

like image 282
Gero Avatar asked Sep 20 '08 19:09

Gero


People also ask

How can I set pagination limit in PHP?

$query = "SELECT * FROM page LIMIT $start, $per_page "; //page bottom, where you want to put your numbers $pages=ceil($num/$per_page); for($s=1; $s<=$pages; $s++) { if($s==$page) $numPage . = "[$s] "; else $numPage . = "<a href='index.

How can I get current page number in pagination PHP?

$currentPage = ceil(($startIndex - 1) / $itemsPerPage) + 1; I used ceil() to make sure you have an integer number, which is rounded up so the current page number is correct.


2 Answers

Use limit in SQL! Every time!

Otherwise you're throwing around considerably more data than you need to, which makes your scripts unnecessarily slow, and will lead to scalability problems as the amount of data in your tables increases.

Limit is your friend!

like image 137
Dan Avatar answered Sep 17 '22 12:09

Dan


Use limit - you don't want to transfer masses of data from the database to the scripting engine if you can avoid it.

like image 21
Quentin Avatar answered Sep 20 '22 12:09

Quentin