Library code snippets

ColdFusion function demonstration

This complete page demonstrates several different Cold Fusion functions:
• Passing form variables
• Database record set outputs
• Using a CF Query
• Using variables

The purpose of this code is to show you how easy it is to use Cold Fusion to create advanced web applications.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
   <!--- By Tim Garver --->
   <!--- www.RINO-IS.COM --->
   <!--- I have an example of this on my site at --->
   <!--- http://www.rino-is.com/webdemos/search.cfm --->
   <!--- I have chosen to call my DSN variable name #mydatasource# --->
   <!--- Read the comments, they will tell you were to enter your own variables to make this work. --->
   <!--- This page is based on fictitious data. So use some common since and make sure --->
   <!--- you rename all of the database field names (field_one, field_two…) to actual field names --->
   <!--- in your database. --->

   <!--- In this example it assumes that the user would only choose one table. If you want them to be able to --->
   <!--- choose multiple tables and use the same output code then you will need to get creative.  --->
   <!--- Or you could Just do IF statements to only show the output for that particular table. --->
   

    <!--- name this page search.cfm --->
   <html>
   <head>
       <title>User defined Query</title>
   </head>
   
   <cfparam name="task" default="">
   <!--- Enter your database DSN name below --->
   <cfset mydatasource = "">
   <!--- End DSN Variable naming --->
   <body bgcolor="#000000" text="#FFFFFF" link="#FFFFFF" vlink="#00FFFF" alink="#00FF00">
   <!--- This if statement sections off the page. if task = nothing then this is the first section --->
   <cfif #task# EQ "">
   <p align="center">Please choose which table to search against.</p><br>
   <p align="center">
   <form action="search.cfm?task=2" method="post">
   <!--- Put your different table names in the options below --->
   <select name="table_name">
       <option value="table_one" SELECTED>Table One</option>
       <option value="table_two">Table Two</option>
       <option value="table_three">Table Three</option>
   </select>
   <input type="submit" value="Next"> <input type="Reset" value="Cancel">
   </form>
   </p>
   <cfabort>
   </cfif>
   <!--- New Task. 2 = page or section 2 they have just submited the first form. --->
   <cfif #task# EQ "2">
   <!--- notice in the form tag the "task" variable!! --->
   <cfform action="search.cfm?task=search" method="post" name="custom" id="custom">
   <cfoutput>
   <!--- The tag below is the variable from the first form. --->
   <input type="hidden" name="table_name" value="#table_name#">
   <!--- We need this in our query --->
   <table>
   <tr>
       <td>
       <!--- This tag is the method of the query. SELECT, INSERT, DELETE, UPDATE ... --->
       <select name="qrymethod"><option selected>SELECT</option></select>
       </td>
       <td>
       <!--- This is what the user can type in for the select statement. --->
               <!--- Unless they know then just leave the *.. --->
       <input type="Text" name="qryselect" value="*">
       </td>
   </tr>
   <tr>
       <td>From:</td>
       <td>
       <!--- This tag stores the prevous table name so the user can see it. --->
       <input type="Text" name="qryfrom" value="#table_name#" readonly>
       </td>
   </tr>
   <tr>
       <td>Where:</td>
       <td>
       <!--- And finally we have the WHERE statement. In the query below, --->
               <!--- we all ready have the "WHERE" in it --->
       <!--- So all the user needs to do here is type in some sort of filter. --->
       <textarea name="qrywhere"></textarea>
       </td>
   </tr>
   </table>
   </cfoutput>
   <p align="center"><input type="Submit" value="Search"> <input type="Reset" value="Cancel"></p>
   </cfform>
   <cfabort>
   <!--- END of the second section or page. --->
   </cfif>
   <cfif #task# EQ "search">
   <!--- Here is our search page output section. --->
   <!--- Here is our query that we are building. --->
   <cfquery name="myquery" datasource="#mydatasource#" dbtype="ODBC">
   #form.qrymethod##form.qryselect#
   FROM #form.table_name#
   #form.qrywhere#
   </cfquery>
   <!--- Put your query output below --->
   <!--- Make sure you use ACTUAL field names. Not the ones below. --->
   <!--- If you want to save space and re-write an output section for each table they want to --->
   <!--- Query, then you will need to get creative and use some tricky technices --->
   <cfif #form.table_name# EQ "table_one">
   <!--- Output if table one is chosen --->
   <table>
   <tr>
       <td>Field One</td>
       <td>Field Two</td>
       <td>Field Three</td>
   </tr>
   <cfoutput query="myquery">
   <tr>
       <td>#field_one#</td>
       <td>#field_two#</td>
       <td>#field_three#</td>
   </tr>
   </cfoutput>
   </table>
   <cfelseif #form.table_name# EQ "table_two">
   <!--- output if table two is chosen --->
   <table>
   <tr>
       <td>Field One</td>
       <td>Field Two</td>
       <td>Field Three</td>
   </tr>
   <cfoutput query="myquery">
   <tr>
       <td>#field_one#</td>
       <td>#field_two#</td>
       <td>#field_three#</td>
   </tr>
   </cfoutput>
   </table>
   <Cfelse>
   <!--- And the other output, if neither one, nor two are chosen --->
   <table>
   <tr>
       <td>Field One</td>
       <td>Field Two</td>
       <td>Field Three</td>
   </tr>
   <cfoutput query="myquery">
   <tr>
       <td>#field_one#</td>
       <td>#field_two#</td>
       <td>#field_three#</td>
   </tr>
   </cfoutput>
   </table>
   <!--- Yes the 3 tables above are Identical, its up to you to make them do what you want them to. --->
   <!--- Enjoy and happy CF Coding. --->
   </cfif>
   <!--- End Query output --->
   </cfif>
   <a href="search.cfm?task=">Start Over</a>
   </body>
   </html>

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of ColdFusion function demonstration.

Leave a comment

Sign in or Join us (it's free).

Tim Garver

Related discussion

Related podcasts

  • Crawlable Flash, Concurrent Python, Smart Robots.txt and more

    Mixed topics today Adobe works with Google, Yahoo and others to make Flash apps crawlable. Reddit.com open sources their codebase "Smart" robots.txt files - do you use them? Bruce Eckel article on concurrent Python with Twisted Railo - open source ColdFusion Cognifty - new PHP framework...

Events coming up

  • Feb 17

    Andrew Powell on How I Learned to Stop Worrying and Love UX

    Atlanta, United States

    Come on out this month to hear Atlanta's own Andrew Powell talk on "How I Learned to Stop Worrying and Love UX"Andrew Powell has been architecting and developing web applications for over 10 years using ColdFusion, Flex, AJAX, and Java. His background includes experience running IT Departments for firms in the executive search and aviation consulting fields. Currently, Andrew is a Principal Architect at Universal Mind specializing in ColdFusion, Java, & Flex.

Want to stay in touch with what's going on? Follow us on twitter!