This tutorial is brought to you by the kind folks at Stranger Studios. We encourage you to check out our other projects.
READ FIRST: I've released a my own modularized version of the pagination code. If you just want to use the function and aren't interested in its implementation, use this code.
- Download the PHP code for the function here: diggstyle_function.txt.
- To use it make a function call like getPaginationString($page, $totalitems, $limit, $adjacents, $targetpage, $pagestring);.
- $page is the current page number
- $totalitems is the total number of items in the set. You usually get this from a SQL query.
- $adjacents is the number of page links to put adjacent to the current page.
- $targetpage is the URL you want to point to. e.g. index.php
- $pagetext is the string used to append the page variable to the URL. e.g. ?page= or &p=
- Use the stylesheet below.
- You may still want to read everything below to understand how the algorithm works.
OTHER VERSIONS: Some awesome folks have created some great stuff based on this code.
- A modularized PHP version, written by Victor de la Rocha at Mis-Algoritmos.com. This code will be much easier to use in your own application. People who just want to download and use some code should visit Victor's page (written in spanish, but you should be able to understand the code). Continue reading below if you are interested in how the algorithm works.
- Also, Mis Algoritmos is at it again with a digg-style pagination plugin for Wordpress based on this code. (more styles)
- A Perl version, written by Andrew. See his write up below.
- A helper for the Zend PHP Framework, written by Rich Milns. See the code at pastie.org.
This page will explain how to build a pagination scheme similar to the one found at Digg.com. Flickr.com uses the same scheme. To be fair, I don't know who first introduced this style of pagination; I just noticed it at Digg first.
Please post any questions or comments at this blog post.
This example is pulling items from our Stranger Studios portfolio. Two items are shown for each page. You can use the pagination object to browse through the pages.
Here is a demo just showing the pagination without any results.
To see a real world example, checkout this list of Oregonian wines at WineLog.net
The main features of this pagination scheme are as follows:
Ok, now that we have that out of the way. Let's build this thing. Our pagination scheme consists of two parts: the CSS and the code to draw the object.
Copy this code into your stylesheet or view source here (change extension to .css on your server).
This part is a little trickier. Below is an implementation in PHP. A good programmer should be able to transpose the code to any other language. If you do, let us know.
There is some potential to take advantage of redundancies, and a definite potential to abstract this into a class or function for general use. I've left these for future exercises.
If you have any trouble implementing the code, let me know and I will try to help out.
Copy the code below into your php file or view source here (change extension to .php to run on your server).
UPDATE: Or download this php code, which includes a function to make using this much easier. Instruction on how to use the function are at the top of the page.
Please post any questions or comments at our corresponding blog post. If you are interested in having Stranger Studios implement a pagination system for your project, contact us here.
Andrew sent me a Perl version of the pagination algorithm. This one is also modularized (how it should be, and how we do it at WineLog now). Below is the email and code Andrew sent me.
Hi Jason,
Thanks for posting your PHP pagination code! I translated it to Perl and modified it and it's very nice.
In my display loop, I show an element if:
($count > ($page - 1) * $numToShow && $count <= $page * $numToShow)
Then I call the modified pagination code:
$output = getPagination(2, $numToShow, $page, $count, $thisScript, '&m=an_extra_mode');
Here is the translated and modified code:
Copy the code below into your cgi script or view source here (change extension to .cgi, etc to run on your server).