views:

64

answers:

3

Hi all, I have an sql query here and it returns a number of results. I'd like to show these results in groups.

What I mean is, show the first 20 results in some part of the page, show the next 20 results in another part of the page etc...

How can I do that?

*I'm using PHP to display results.

+1  A: 

What you want is called pagination and the specific implementation depends on the database. For example, in MySQL you can use LIMIT a,b, and most other databases you can use either TOP(n) or ROW_NUMBER.

Mark Byers
a) I'd like to show all the results in the same page, not in several pages. b) By using the LIMIT command, should I create several sql queries? One query for each group of results? That wouldn't be easy because I don't know the number of results.
ktsixit
I think, the author should use his php to group results somehow. We often use ordered maps for this task. Some technologies, like Scala, even provide convenience methods on collections that group the elements into a map using specified predicate. I am not aware of such thing in php.
incarnate
Yes, I guess if you are always going to show all the results on the same page then you can query for them all and use PHP to split them into 20 rows at a time.
Mark Byers
I understand that, but my question is about how to do that
ktsixit
@ktsixit: Iterate over the rows one by one and when you reach 20, stop iterating.
Mark Byers
A: 

do your SELECT command with LIMIT statement.

on first query you can get first 20 results, then next 20, etc.

Axarydax
A: 

Ok I found a solution in a forum. It's here in case somebody else needs it

http://www.phpbuilder.com/board/showthread.php?t=10311631

ktsixit