Tutorials

PHP Basic Pagination

by Crayon Violent on Jun 18, 2008 6:11:55 PM

Okay before anything else, here is the code in all it's full and basking glory, because there's nothing more I hate about tutorials than how people like to break them all into itty bitty little chunks and you have to copy and paste, copy and paste, copy and paste. It's aggravating....and yet it's somewhat contrary to the theme of this tutorial. Isn't it ironic?

Okay so there's the code. Basically the idea is to:

- Find out how many rows you have in your table
- Find out how many pages you want to make, based on how many rows per page you want to show
- Find out what page we're on
- Pull out just the rows of the current page
- Display the info
- Make some links to go to the first page, previous page, some pages around the current page, the next page, and the last page.

There's comments aplenty in the code, so you may be able to figure out what's going on based on that, but let's go ahead and break it down for some further commentary. Moving on...

Comments

Thank god for that. I'm tired of having to tell people to google for pagination tutorials rather than being able to point them to something specific!

Reads well CV.

1. Ben Smithers on Jun 19, 2008 8:47:14 AM

You said there's nothing more annoying then having to copy and paste, copy and paste, copy and paste. But the point is you're not learning anything when you do it that way so they make it hard to just cheat and not learn.

2. Rezert on Jun 20, 2008 8:48:44 AM

If someone is looking to cheat, they would just disregard the commentary and copy and paste it anyways. I'm not in the business of "forcing" people to learn. I'm in the business of giving people the material to learn, the best way I know how.

If someone feels they need to be walked through each piece of code, it's there for them. If someone feels they can learn it by just looking at the code, it's there for them. If someone feels they can just c/p it into their code and hope some magic wand is waved...again, that's their prerogative.

It is certainly my choice to make tutorials, but if I do endeavor to do so, it is no longer a choice, but a responsibility to make the resource as available as possible. It is at no point in time my place to police how people use that information. In fact, I refuse to accept responsibility for that, at all. How do I know that someone will not use it for malicious purposes? Should I be held responsible for that? I think not!

3. Crayon Violent on Jun 20, 2008 10:35:51 AM

I love the commentary on the tutorials. If I get stuck I always refer back to it. That being said I like the idea of having all the code up front so I can tinker and screw it up on my own. It's how I learn best.

Thanks for the great tutorial btw.

4. Rodney Moore on Jun 20, 2008 3:45:30 PM

Point well stated.

5. Rezert on Jun 20, 2008 7:38:02 PM

I cannot get this code to work. Has anyone encountered the same problem?

6. norwid on Jun 22, 2008 1:50:14 PM

can you be more specific?

7. Crayon Violent on Jun 22, 2008 4:48:14 PM

Thank you CV great code

8. Christopher Burnside on Jun 24, 2008 7:07:35 AM

Thank you Crayon Violent for the most excellent tutorial...
Very simple to follow, the only real changes that I had to make were, instead of using "{$_SERVER['PHP_SELF']}?", using "{$_SERVER['REQUEST_URI']}&" so it would tack on the pagination to my already existing stream of $_GET variables...

9. webent on Jul 3, 2008 4:08:16 PM

SQL_CALC_FOUND_ROWS can be used under MySQL instead of querying the first SELECT COUNT(*) FROM NUMBERS

10. petitchevalroux on Aug 1, 2008 8:02:26 AM

thank you so much!! just what i needed... been searching the web for 3 hrs... i really appreciate it. now i can finally finish my project... thanks a bunch CV!!!!

11. vherhonne on Aug 2, 2008 12:40:41 PM

This tutorial was great in helping solidify my pagination technique! Easily explained for dummies as well, always helps.

12. GreenUser on Aug 15, 2008 3:19:29 PM
Login or register to post a comment.