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>

You might also like...

Comments

Tim Garver

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”