473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


ColdFusion Language

By Max Kington
Senior Programmer, Webhanger.com

The Language

The ColdFusion language falls into one of two categories depending on who you are.

If you're an HTML god who has difficulty conversing with other Human beings without saying close tag at the end of every sentence there is a good chance that you'll like ColdFusion.

If on the other hand you prefer to talk to people about floating point numbers and just how useful 2 dimensional arrays are than you might find learning ColdFusion a bit of a drag.

It all depends on how you like to look at logical ways of doing things. The ColdFusion Mark-up Language or CFML is based on tags.

The following code would retrieve information from a database and output it in a table. I'll explain each piece of code later.

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <html>
3 <head>
4 <title>My First ColdFusion Template</title>
5 </head>
6 <body>
7 <cfquery name=myquery datasource=MySystemDSN>
8 SELECT * FROM mytable
9 </cfquery>
10 11 <cfoutput query="myquery">
12 <tr>
13 <td>#fldone#</td>
14 <td>#fldtwo#</td>
15 <td>#fldthree#</td>
16 </tr>
17 </cfoutput>
18
19 </body>
20 </html>

Look Puzzling? It shouldn't do! At least, not too puzzling. Let's break down what we have here.

Lines 1-6:

This shouldn't be anything new; as this is standard HTML setting up the document as you would any other Web page.

Lines 7-9:

This is the first piece of ColdFusion code you encounter. The cfquery tag is the basis for any retrieval of data from a database.

First of all you have to call your query something so that you can access the data that it retrieves.

<cfquery name=myquery……..

Second, you open the connection to a database, I use the Windows version of ColdFusion server so in the same way that Active Server Pages connects to a database, you create a system Datasource and point that at your Database. You then tell ColdFusion to connect to that Datasource

<cfquery name=myquery datasource=MySystemDSN>

Thirdly you need to write an SQL statement to get the records out of the database you want. Please note that this is a Structured Query Language statement, not ColdFusion, if you want more info on SQL come back soon to thescripts.com and checkout the new ColdFusion articles where I'll do some more SQL.

SELECT * FROM mytable

This statement will Select all (by using *) from a table called mytable. More complicated SQL will be covered in later tutorials

.Last but not least you have to close the cfquery tag.

</cfquery>

The cfquery tag is far more powerful than this, it has more options and attributes and I've only showed you a small amount to help you understand what it's about.

Lines 11-17:

This is a mixture of ColdFusion and HTML but the tag we really want to look at is cfoutput tag as this is what you have to use if you want to output any kind of ColdFusion enhanced data. cfoutput can be used in its very basic form in the form of:

<cfoutput>

</cfoutput>

However, when it's tied with a query as detailed above it becomes a lot more powerful. It will allow you to access all of the data from that query. In our example this means any of the fields in "mytable". When used like this cfoutput will get all of the variables from one record and then go onto the next record.

21 <cfoutput query="myquery">
22 <tr>
23 <td>#fldone#</td>
24 <td>#fldtwo#</td>
25 <td>#fldthree#</td>
26 </tr>
27 </cfoutput>

The values that are inside the Pound symbols ## (I call them hash symbols because I come from the UK but there you go) represent variables, hence when the code is execute they are replaced by the value of that variable.

In this example the query we did earlier got all of the records from mytable, in that table there were three columns, fldone, fldtwo and fldthree, we access the data in there using variables. E.g. #fldone#, #fldtwo# etc.

So this code would take the values of fldone fldtwo and fldthree of the first record place them into the table columns, and then repeat for record two until all records were output.

See, easy isn't it?

« Background  

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.