Way back, I wrote the tutorial on how to create a pagination scheme similar to the one that Digg uses. The code in the tutorial was meant to educate readers on how the algorithm works.

Meanwhile, I was using completely different code for my own projects. Typically I put all of the pagination code into a function (or PHP class), which makes it much easier to use in practice. Now I’ve made available a version of this function, which people should look to first if they want a quick implementation.

  • 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 this style sheet.

Read the tutorial for more help and to see an example. For a real-live example, check out my Wine Log.

Also, Mis Algoritmos is at it again with a digg-style pagination plugin for Wordpress based on this code.