Ok, somehow I’m doing some ColdFusion development. Not sure how I got into this, but I never turn down an opportunity to play with something new. After a quick ColdFusion 101 guide from a guru at work, I was left to my own devices to try and develop a little application that has been towards the bottom of someones to-do list for quite a while.
I’m not going to going into the details of the application in this blog post, but I’m going to walk through the steps involved in getting started with ColdFusion and some various techniques. Please note I really am Class 101 with ColdFusion, so some of the stuff I’m doing here may be wrong. I’m just documenting what I’ve done. If you spot any errors, or improvements, really please let me know – I’d be delighted to learn!
Just before getting started, apologises if this post goes on. I’m attempting to document my actions, and I’m not sure how lengthy this will get. Also, much of this information is available from Google searches, but I’m simply posting this as a reference for myself – hopefully you mind find certain sections useful.
1. Installation of ColdFusion
Adobe allow personal developers to download a free version of the software. Head over to Adobe and download your version (think you need to sign up for an Adobe account). And then install it.
2. Check It Works
Depending on some options entered during installation you may have slightly difference entries here. I think for the most part I selected the default options. My installation went to C:\ColdFusion9, and within this directory is wwwroot. In here I created a simple little Hello World! html page:
<html> <head> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> </body> </html>
And then browsing to http://127.0.0.1:8500/hello.html I get the page as expected:
3. Check ColdFusion is Working
To try the next simplest thing to check that ColdFusion was working I decide to try the cfouput tag and user the uppercase function to alter the text displayed to be all in uppercase. For this I edited my hello.html file to be as follows, and then altered the file extension to .cfm.
<html> <head> <title>Hello World!</title> </head> <body> <h1><cfoutput>#UCase("Hello World!")#</cfoutput></h1> </body> </html>
And this ouput the same as the hello.html page previous displayed, except with the message in uppercase characters.
4. Passing variables from one form to the next
After throwing together a little form to take some input from the user, I then wanted to handle the form submit, and retrieve those values on the next page.
To investigate this I wrote a little form that simply took the persons name, and then called outputName.cfm when the user pressed the Submit button.
The source code for entername.cfm is:
<html> <head> <title>Name Entry</title> </head> <body> <h1>Enter your name</h1> <cfform method="post" action="outputname.cfm"> Please enter your name: <cfinput type="text" name="name" size="20" required="yes" validateat="onsubmit" message="You must enter your name."><br /> <br /> Click to submit your name <br /> <input type="submit" value="Submit" /> </cfform> </body> </html>
And the sourcecode for outputname.cfm is:
<html> <head> <title>Output name</title> </head> <body> <h1>Output Name</h1> <br/> You entered : <cfoutput>#form.name#</cfoutput> <br /> </body> </html>
And the ouput from this can be seen below:
5. Using Functions
The next thing on my to do list for the application is to write and read data from a database. Rather than embed the database code into my pages, I’d rather make a seperate file to contain all this information, effectively introducing an extra tier into my application. So I needed to look into calling functions from other files. For this I read about include files, and storing include files outside of ‘wwwroot’ was the recommended route for security purposes.
Above the ‘wwwroot’ folder I created a ‘common’ folder and then created a new file called ‘dbAccess.inc’. In this file I created a simple ‘add’ function. Sourcecode follows:
Sourcecode for dbAccess.inc
<cffunction name="addNums" returntype="numeric"> <cfargument name="num1" type="numeric"> <cfargument name="num2" type="numeric"> <cfset sum = num1 + num2> <cfreturn sum> </cffunction>
I then updated my outputname.cfm to include the file dbAccess.inc, and then call the function and output the result. The updated sourcecode follows:
<html> <head> <title>Output name</title> </head> <body> <cfinclude template="../common/dbAccess.inc"> <h1>Output Name</h1> <br/> You entered : <cfoutput>#form.name#</cfoutput> <br /> <cfset total = addNums(1,2)> The call to the function 'Add' returned: <cfoutput>#total#</cfoutput> </body> </html>
The resulting output was then:
6. Database Interation
My first step was to create a Data Source via the ColdFusion administrator panel – namely GBTest. I then added the following ‘cfquery’ to my dbAccess.inc file. This query simply returns the number of rows in a particular table.
<cfquery name="getRowCount" datasource="GBTest"> SELECT count(*) as mycount FROM GregTest </cfquery>
After ensuring this worked – well it returned 0 rows for an empty table, I then looked into how to popualte some data.
7. Database – Inserting Rows
For this step I need to talk to our local ColdFusion guru. I had found examples of how to do this on the net, but didn’t want to move away from my include file – turns out I needed to use a function within a ColdFusion component.
To create a component, I created a new file dbAccess.cfc and the source looks as follows:
<cfcomponent> <cffunction name="insertRow" access="public"> <cfargument name="userid" type="numeric" required="yes"> <cfargument name="entrytype" type="numeric" required="yes"> <cfargument name="time" type="string" required="yes"> <cfargument name="date" type="string" required="yes"> <cfquery name="insertRow" datasource="GBTest"> INSERT INTO GregTest VALUES (#userid#, #entrytype#, '#time#', '#date#') </cfquery> </cffunction> </cfcomponent>
And then to call it, I altered the cfinclude tag to point to the cfc file. The invoke code looks like this:
<cfinvoke component="dbAccess" method="insertRow"> <cfinvokeargument name="userid" value="3"> <cfinvokeargument name="entrytype" value="1"> <cfinvokeargument name="time" value="#form.starttime#"> <cfinvokeargument name="date" value="23-FEB-11"> </cfinvoke>
Finally for this blog post I’m going to look into outputting some data from the database using the cfc file. Very rough and ready but this appears to be working for me. Update to the .cfc file:
<cffunction name="retrieveData" access="public" returntype="query"> <cfargument name="userid" type="numeric" required="yes"> <cfquery name="qGetData" datasource="GBTest"> SELECT entrytype, timeentered, entry_date FROM GregTest </cfquery> <cfreturn qGetData> </cffunction>
And update the the .cfm page:
<cfinvoke component="dbAccess" method="retrieveData" returnvariable="qData"> <cfinvokeargument name="userid" value="3"> </cfinvoke> <cfdump var="#qData#">
And I think that concludes my ColdFusion 101 post.