views:

46

answers:

1

The way I am doing my pagination now is as follow:

===============
First I do the normal query, and get all the result back.

After that I count the results, lets say 300. I do this with num_results

Now I check what page the user wants, and the limit, lets say 30,10.

Last I do the new query, with the limit selected.

===============

Using this method I do almost the same query twice, is there no other way to do this, in just one go.

+2  A: 

Yes, you can use the SQL_CALC_FOUND_ROWS for exactly this purpose.

select SQL_CALC_FOUND_ROWS something from table limit 30,10

This way you can still get the amount of rows normally retrieved when NOT using the limit clause.

Dennis Haarbrink
Wow nice I will try this out!
Saif Bechan
@Dennis Haarbrink: I think this may introduce a chicken and egg situation. If the row count is required to calculate the pagination sets to be displayed (let's say 30 pages each with 10 items), then the row count must be returned first. At this point, the user can pick which page to view. Using `SQL_CALC_FOUND_ROWS` means that the row count is returned *after* the user has selected which page to view.
Mike
@Mike: I would assume that the number of items per page is a 'constant' and together with the total row count you can determine the number of pages. So let's say you want page#3, the offset becomes `$num_pages * $records_per_page`.
Dennis Haarbrink
@Dennis Haarbrink: My thought was that until you have the total number of pages, you can't ask the user which one of those pages they want to see. So, to display the number of pages, you have to get the row count from the database and calculate the number of pages. You then ask the user which page they want, and go back to the database for that page.
Mike
@Mike: Well, usually you present the user with first page and from there let the user choose where to go. So your 'base' offset will always be zero.
Dennis Haarbrink
Hi I am not sure what this discussion is about, but the method Dennis assumes works great. I just run the query the user wants, with the limit. And after that I can get the number of rows there would be if I had not used a limit. Thanks
Saif Bechan
@Dennis Haarbrink: I think we may have our wires crossed :-) If you don't know how many records there are, then you cannot calculate how many pages there are. If you don't know how many pages there are when you display the first page, how can you present the user with a list of pages from which they can choose? Before the user can pick a page, they need a list of pages from which to pick. Admittedly, this point does seem somewhat academic, as your solution has been accepted as working...
Mike
@Mike: Hehe, allright let me try to explain by describing a scenario. The user enters a page where a paginated list of items is displayed. The script executes a query getting the first **x** results (offset=0) and calculating the total no rows. When that result returns you have your first page of items + you know the total amount of rows, thus you can calculate the total number of pages. I guess our discrepancy is that I assume that the user enters on page 1, while you assume the user chooses beforehand. In the latter case you'd first need to get a count(*).
Dennis Haarbrink
@Dennis Haarbrink: (sound of penny dropping) Ah! I see what you mean. Cunning! Thanks for your patience, and the explanation. I think I'll be using that technique in future.
Mike
@Mike: Happy To Help!
Dennis Haarbrink