Pagination question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
siko
Forum Commoner
Posts: 37
Joined: Tue Feb 16, 2010 11:28 pm

Pagination question

Post by siko »

Say if I have a search page on my site that allows many search options and criterias, and results are generally a couple of hundreds records for each search, pulled from a database of around 20,000 records, and I'll need to paginate the results.

I'm thinking of the following algorithm, which is what most PHP pagination scripts I've found out there are doing:

Search 20,000 records based on specified search criteria and count the number of results.
Total count of results / Number of records I want to display per page, and display the pagination links accordingly.
Display results for the current page.

That pretty much works, but the thing is.. seems like I'll have to peform the search again everytime I want to look at another page of the results, in which case I'll have to somehow pass my search criterias for this search through GET or POST in order for the search to be done again? Is it supposed to be this way?
User avatar
JAY6390
Forum Newbie
Posts: 20
Joined: Sat Apr 17, 2010 6:51 am
Location: UK

Re: Pagination question

Post by JAY6390 »

Hi Siko

Yes that is pretty much the way it will work. You could of course just save the criteria in a session variable and just have a checksum passed via GET or POST to verify it's the same search query

Also, if you'd like an easy way of creating pagination results with PHP take a look at my simple php pagination class

Regards

Jay
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Pagination question

Post by social_experiment »

siko wrote:Search 20,000 records based on specified search criteria and count the number of results.
Total count of results / Number of records I want to display per page, and display the pagination links accordingly.
Display results for the current page.
You should use the following instead :

1.Count the records using COUNT() [assuming you are using sql] with your search query
2.Total count of results / Number of records per page
3.Search query (get results)
4.Display results
5.Create pagination links
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply