Home arrow ColdFusion arrow Page 2 - Creating NextPrev Link in Cold Fusion
COLDFUSION

Creating NextPrev Link in Cold Fusion


Creating a Next/Prev link for query results in Cold Fusion is very easy with <cfloop> tag. Passing parameters to Start row and End row limits the record display in the specified range. Almost all of our database- driven applications require paging of query results. It might be handy if we had a custom tag or script written already. Just what we need is to extend that code wherever we want to place the paging and then change a few parameters.  In this article, I show you how we can create Cold Fusion database query results with "Next 1 2 3 4... Prev" links.

Author Info:
By: Shiju Rajan
Rating: 5 stars5 stars5 stars5 stars5 stars / 59
March 31, 2004
TABLE OF CONTENTS:
  1. · Creating NextPrev Link in Cold Fusion
  2. · Calculate the Offset and Limit Rows
  3. · Pass More Parameters to Create/Prev Link

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
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...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials