ColdFusion
  Home arrow ColdFusion arrow Page 3 - 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 - 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.

    Example.cfm

    <form method="post" action="<cfoutput>#cgi.script_name#</cfoutput>">
    Search 
    : <input type="text" name="frmsearch" size="25">
    <input type="submit" name="frmbtn" value="Go">
    </form>
    <br>
    <br>
     
    <!--- 
    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 "">
    where 
    (name like '%#url.frmsearch#%' or
                dept like 
    '%#url.frmsearch#%')
    </cfif>
    </cfquery>
     
    <!--- 
    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# 
    </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## lcl_linkpara#">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## lcl_linkpara#">#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## lcl_linkpara#">NEXT</a></cfoutput
    </cfif
    </cfif
     
    <!--- 
    display the result on the screen ---> 
    <cfloop query="q_fetch" startrow="#URL.offset#" endrow="#limit#"
    <cfoutput>#id# - #name# - #dept#</cfoutput> <br>
    </cfloop> 

    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.

     

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek