Home arrow ColdFusion arrow Page 3 - Creating NextPrev Link in Cold Fusion

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
  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

Creating NextPrev Link in Cold Fusion - Pass More Parameters to Create/Prev Link
(Page 3 of 3 )

This is a basic example of how to create paging in Cold Fusion. If you are using complex SQL queries such as a search with above <cfquery>, you need to make sure that you are passing the parameters to the next/prev hyper links.

Here is an example of a small search action on employee data. First just give an option for user to put in the search criteria.


<form method="post" action="<cfoutput>#cgi.script_name#</cfoutput>">
: <input type="text" name="frmsearch" size="25">
<input type="submit" name="frmbtn" value="Go">
search parameters --->
<cfparam name="form.frmsearch" default="">
<cfparam name="url.frmsearch" default="#form.frmsearch#">
query for pulling employee details --->
<cfquery name="q_fetch" datasource="mydsn"
select id
namedept from employee
<!--- user has entered a search criteria so create a where condition   --->
<cfif trim(url.frmsearchneq "">
(name like '%#url.frmsearch#%' or
            dept like 
create a parameter to pass that to the links --->
<cfset lcl_linkpara="&frmsearch=#urlencodedformat(url.frmsearch)#">
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# 
<!--- 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
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## lcl_linkpara#">PREV</a></cfoutput
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><a href="#cgi.script_name#?offset=#offset_value## lcl_linkpara#">#i#</a></cfoutput> 
<!--- 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## lcl_linkpara#">NEXT</a></cfoutput
display the result on the screen ---> 
<cfloop query="q_fetch" startrow="#URL.offset#" endrow="#limit#"
<cfoutput>#id# - #name# - #dept#</cfoutput> <br>

Winding Things Up

There are a couple of things about this example before I wind up this article. First, it is creating a WHERE condition in the sql statement if search parameter is passed. Secondly, it creates a link parameter and passes the correct information to the paging hyperlinks. It uses the URLEncodedFormat function to pass arbitrary strings within a URL so the query string passes through the different browser type safely. Cold Fusion automatically decodes URL parameters that are passed to the template while receiving through the URL query string.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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