Simple Pagination Algorithm using ColdFusion

For one of my projects, I needed a simple pagination module. The requirements were pretty standard:

  • The first and last page should always be displayed
  • There should always be the same number of pages displayed for consistency (+- 1)
  • There should be maximum code reuse
  • It should be easily configurable
  • The current page should stand out

So here is my answer to this problem. Even if you're not familiar with coldfusion, you should be able to understand the code. (just mentally replace <cfif> by "if",<cfloop> by "for", etc.)

CFM:
  1. <!--- this script assumes you get the currentPage and lastPage variables from somewhere else --->
  2. <!---  number of pages to display, excluding first and last --->
  3. <cfset pages=4>
  4. <cfset beforeSeparator="">
  5. <cfset afterSeparator="">
  6. <!--- limit after which to insert a separator (total number of pages spanided by two) --->
  7. <cfset limit=Int(pages/2)>
  8. <!--- close to beginning, hide later pages and insert separator after --->
  9. <cfif currentPage lte limit>
  10.   <cfset start=2>
  11.   <cfset end=pages+1>
  12.   <cfset afterSeparator="...">
  13.   <!--- close to end, hide first pages and insert separator before --->
  14.   <cfelseif currentPage gte lastPage-limit>
  15.   <cfset start=lastPage-pages>
  16.   <cfset end=lastPage-1>
  17.   <cfset beforeSeparator="...">
  18.   <!--- in the middle, hide pages before and after and insert separators --->
  19.   <cfelse>
  20.   <cfset start=currentPage-limit>
  21.   <cfset end=currentPage+limit>
  22.   <cfset beforeSeparator="...">
  23.   <cfset afterSeparator="...">
  24. </cfif>
  25. <!---  first page --->
  26.  
  27. <cfif currentPage eq 1>
  28.   <span class="pagenumber_current"> 1 </span>
  29.   <cfelse>
  30.   <span class="pagenumber"> <a href="index.cfm?page=1">1</a> </span>
  31. </cfif>
  32. #beforeSeparator#
  33. <!--- loop --->
  34. <cfloop index="i" from="#start#" to="#end#">
  35.   <cfif currentPage eq i>
  36.     <span class="pagenumber_current"> #i# </span>
  37.     <cfelse>
  38.     <span class="pagenumber"> <a href="index.cfm?page=#i#">#i#</a> </span>
  39.   </cfif>
  40. </cfloop>
  41. #afterSeparator#
  42. <!--- last page --->
  43. <cfif currentPage eq lastPage>
  44.   <span class="pagenumber_current"> #lastPage# </span>
  45.   <cfelse>
  46.   <span class="pagenumber"> <a href="index.cfm?page=#lastPage#">#lastPage#</a> </span>
  47. </cfif>

P.S.: if you find your life boring and lacking adventure, try getting ColdFusion code to display right in WordPress. I promise it will get your adrenaline levels up in no time.



7 Thoughts

  1. Murtaza says:

    nice tutorial thanks

    ————————
    http://www.pakbridge.com

  2. Sacha says:

    Thanks for you support !
    When I have time I will redo this code snippet better, maybe using javascript or php instead of coldfusion, and providing illustrated examples along the way.

  3. Valtee says:

    Thanx for the code. Now i am starting to feel how nice coldfusion is.

  4. Valtee says:

    Have to revisit this once again. Just wondering where the 1st and last pages are coming from

  5. Sacha says:

    In this script, I assume the first page is simply “1″.

    The “last page” variable has to be defined before my code. Usually this variable will depend on outside factors, like your database’s content, or search results number, etc.

  6. Francesco says:

    Bad code, there are some variable that should be set before, and it needs some cfoutput…

    Good tutorial however…

  7. Sacha says:

    Feel free to post your solution. I haven’t done any coldfusion for the last year, so I kinda forgot the syntax..

Your Thoughts...


All content © Copyright 2010 by Sacha Greif.
Subscribe to RSS Feed – Posts or just Comments

Powered by WordPress
Designed by Graph Paper Press
Redesigned by Me