ColdFusion
  Home arrow ColdFusion arrow Page 2 - Creating NextPrev Link in Cold Fusion
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
COLDFUSION

Creating NextPrev Link in Cold Fusion
By: Shiju Rajan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2004-03-31

    Table of Contents:
  • Creating NextPrev Link in Cold Fusion
  • Calculate the Offset and Limit Rows
  • Pass More Parameters to Create/Prev Link

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating NextPrev Link in Cold Fusion - Calculate the Offset and Limit Rows


    (Page 2 of 3 )

    Following is the code for splitting the page numbers and finding the offset values for each link. I have written comments for each and every line so you should be able to understand the logic clearly. 


    <!--- set how many records you want to display per page ---> 
    <cfset Result_Per_Page="10"
     
    <!--- 
    get the total record count from q_fetch query --->
    <cfset Total_Records="#q_fetch.recordcount#"
     
    <!--- 
    set the default value for the offset record set number ---> 
    <cfparam name="URL.offset" default="0"
     
    <!--- 
    the limit result set(i.e., end row) ---> 
    <cfset limit=URL.offset+Result_Per_Page
     
    <!--- 
    page results start from? ---> 
    <cfset start_result=URL.offset+1
     
    <!--- 
    showing results 1 10 of total record --->
    <cfoutput>Showing results #start_result# - 
    <cfif limit GT Total_Records> #Total_Records# <cfelse> #limit# </cfif> of #Total_Records# 
    </cfoutput> 
     
    <!--- make sure that the initial start row is starting from 1 ---> 
    <cfset URL.offset=URL.offset+1
     
    <!--- if 
    the record is their more than one page so show the navigation bar ---> 
    <cfif Total_Records GT Result_Per_Page
    <br
     
    <!--- 
    Create Previous Link ---> 
    <cfif URL.offset GT Result_Per_Page
    <!--- Previous Link Offset ---> 
    <cfset prev_link=URL.offset-Result_Per_Page-1
    <cfoutput><a href="#cgi.script_name#?offset=#prev_link#">PREV</a></cfoutput
    </cfif
     
    <!--- 
    Find out how many pages are there for display  ---> 
    <cfset Total_Pages=ceiling(Total_Records/Result_Per_Page)> 
     
    <!--- 
    now loop it for navigation page numbers ---> 
    <cfloop index="i" from="1" to="#Total_Pages#"
    <cfset j=i-1
    <!--- create offset value for page numbers ---> 
    <cfset offset_value=j*Result_Per_Page
     
    <!--- 
    deactivate the link if the page number is current page ---> 
    <cfif offset_value EQ URL.offset-
    <cfoutput>#i#</cfoutput> 
    <cfelse> 
    <cfoutput><a href="#cgi.script_name#?offset=#offset_value#">#i#</a></cfoutput> 
    </cfif> 
    </cfloop> 
     
    <!--- create Next Link ---> 
    <cfif limit LT Total_Records
    <!--- Next Link Offset ---> 
    <cfset next_link=URL.offset+Result_Per_Page-1
    <cfoutput><a href="#cgi.script_name#?offset=#next_link#">NEXT</a></cfoutput
    </cfif
    </cfif

    In the above code I used the ceiling function to calculate the Total number of pages. Ceiling function returns the closest integer to the passed value. Also, I used a cgi.script_name variable to pass the current Cold Fusion page name.

    Seeing the Results

    Now we need to print the query results on the screen.


    <!--- display the result on the screen ---> 
    <cfloop query="q_fetch" startrow="#URL.offset#" endrow="#limit#"
    <cfoutput>#id# - #name# - #dept#</cfoutput> <br>
    </cfloop> 

    <cfloop> tag calls the query 'q_fetch' and limits the query result based on the values passed through offset and limit variables.

    More ColdFusion Articles
    More By Shiju Rajan


     

    COLDFUSION ARTICLES

    - Adobe ColdFusion Just Got More RAD
    - How to Access a SQL Anywhere Database with C...
    - CFXML: Probing XMLDOM in ColdFusion
    - Creating a Web Service with ColdFusion: the ...
    - CFAjax: What it is and How to Use it
    - Querying SQL 2000 Server from ColdFusion
    - Introduction to ColdFusion Markup Language, ...
    - Introduction to ColdFusion Markup Language
    - Databases and Dreamweaver MX 2004, concluded
    - Databases and Dreamweaver MX 2004
    - Welcome to Coldfusion MX 6.1, concluded
    - Welcome to Coldfusion MX 6.1
    - What You Must Know About ColdFusion Flow-Con...
    - What You Must Know About Operators in ColdFu...
    - Everything You Must Know About ColdFusion Va...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT