Question by ampersand1: What is a good, simple php script for paginating mysql results?
I need to find a way to get results from php queries onto paged results pages. I need to know where to put the pagination script, i.e. does it go in the original script, is it all seperate, etc.?
Answer by sk89q
First of all, keep track of the current page number. You can do this in the URL (for example: results.php?page=1).
Then, when you load from the database in results.php, take note of the current page and then added a LIMIT clause to your query. For example, if you were showing 100 results per page and you were on page 2, you would start from the 100th entry instead of the 0th entry. (Remember that page 1 and “page 0” are the same). To put the list of click-able page numbers, you need to get the total number of results and find out how many pages there are.
You can do the query and the page logic in the same script. Get the page number, figure how many records to script, limit the query by using SQL’s LIMIT, then display the results and the list of page number links.
There’s another way to do this too instead of using page numbers. Many software programs, instead of passing along the page number (such as results.php?page=15), pass along the starting index of results (like results.php?start=100). It would start after 100 results for example.
What do you think? Answer below!
There are no revisions for this post.