Published on: Sunday 25th October 1998 By: Janus Boye
In this first article about Cold Fusion from Allaire, I'll try to give you a short introduction to application servers, and I'll even walk you through creating your very first .cfm page, that will let you select and output data from a database. In the .cfm page, CFML (Cold Fusion Markup Language) is used, alongside with HTML. CFML looks very much like HTML, just with some interesting extended capabilities. More about that later.
Please refer to the What do I need? section, to take a look at the prequisites of Cold Fusion and the example covered in this article.
An application server, is a server, that so to speak, lies behind your webserver. If you imagine yourself, through your browser, accessing a .cfm page. What will happen, is that a request, from your browser, will be sent to the webserver requesting the .cfm page. The web server has then been told by the application server, in this case Cold Fusion, to let the Cold Fusion App. Server handle .cfm pages, so the webserver (e.g. IIS, Netscape Enterprise) simply just passes on the request to the application server, which then does the actual work.
When the application server receives the request, it then starts processing the page. Any Cold Fusion code is executed, and it then dynamically creates the .cfm page (with 100% HTML code), and then sends this to the server, which quickly passes it onto the client - in this case you.
When the page is fully loaded in your browser, you could take a look at the code, and you'll notice, that it is normal HTML (If there is a such thing as normal HTML...). The only thing different from normal .htm pages, is that this .cfm page has been dynamically created by the Cold Fusion App. Server. The big advantage is, that if you updated the table in your database, the .cfm would then automatically be re-created at the next request.
Cold Fusion is an application server from Allaire. It is now more than 3 years old, so it is a bit more mature than many other application servers out there. Version 4 has just been released.
Cold Fusion can be installed on Microsoft Windows NT or Sun Solaris, works with several webservers, and can access several different databases (including Oracle, Sybase, Microsoft SQL, Access).
It is most important that you have server access. You also need a server with Cold Fusion installed (can be downloaded from Allaire's website), and last, but not least, you do need a browser.
Before we make anything dynamic, you need to either create a database, or have a database ready. Cold Fusion supports most databases, including Oracle, Sybase, Microsoft SQL and Access and many more.
To get the example to work, you need to have a table named Members in your database, with the following fields: ID, Name, Age. To give the .cfm some sense, you should also seriously consider adding some data to your table. E.g. 3 IDs, Names and Ages....
Then, you need to define the datasource on your server. In Microsoft Windows NT, this is done in the Control Panel under ODBC Sources. In the example below, I've named my datasource irt.
When this is done, it is normally a good example to open your browser, and go to your Cold Fusion Administrator homepage (normally @ http://servername/cfdocs - where servername is the name of your server with Cold Fusion installed). After you've logged into the administrator page, you can then verify that your connection to your database is working.
When this is done succesfully, you are then ready to move onto the example.
In this example, we'll create a .cfm page (members.cfm), that will request data from a table called Members in my irt datasource.
Basically, what I want, is a .cfm page, that will give me a up-to-date listing of all members in my database.
To do this, I've created members.cfm, that holds the following code (feel free to copy it onto your editor):
<!--- This query selects all members from the Members table in the irt datasource ---> <CFQUERY NAME="GetMembers" DATASOURCE="irt"> SELECT * FROM Members </CFQUERY> <html> <head> <title>Members of irt.org</title> </head> <body> <table border="1"> <tr> <th>ID</th><th>Name</th><th>Age</th> </tr> <!--- Here's all members output nicely into a table ---> <CFOUTPUT QUERY="GetMembers"> <tr> <td>#ID#</td><td>#Name#</td><td>#Age#</td> </tr> </CFOUTPUT> </table> </body> </html>
To improve readability I've put all regular HTML in smallcaps.
The first thing you'll notice, on the very first line of members.cfm, is that CFML comments, has three slashes (<!---), unlike HTML comments that only have two slashes (<!--). In .cfm pages you can use both, but CFML comments remain hidden to the user. From the very beginning it is a good idea to comment your code heavily, as this will make things much easier for you as you move along.
After the comment, I use the CFQUERY tag to SELECT all fields (*) from my irt datasource. I baptize the query: GetMembers. If you know SQL, you'll notice that everything between the opening CFQUERY and the closing CFQUERY is normal SQL.
After that follows some regular HTML code, a HTML-tag, BODY-tag, TITLE-tag, and then I create a table, with 3 rows: ID, Name and Age.
I then close the table row (</tr>) and use CFOUTPUT to output all data from my GetMembers query earlier on.
When the Cold Fusion App. Server reaches CFOUTPUT, it knows that everything embedded in # characters, is to be treated as variables. #ID# (GetMembers.ID), will then be the ID of the very first member in the database. #Name# will then be the name of the very first member in the database, and #Age# will then be the age of the very first member in the database.
After having outputtet these 3 values (might look something like this):
Cold Fusion then reaches the closing of the CFOUTPUT tags. If there are no more records to display, Cold Fusion finishes processing the page, or else it just displays all the other records. Please note, that for each and every record, Cold Fusion will move down a table row, which will make the page look nice.
If you have done everything correctly, you should now have your very first .cfm page. Try to take a look at the source code!
If you have a huge database, loaded with records, you might consider changing the SQL inside CFQUERY a bit, to improve processing time, and make your members.cfm more scalable.
Try changing from:
<CFQUERY NAME="GetMembers" DATASOURCE="irt"> SELECT * FROM Members </CFQUERY>
<CFQUERY NAME="GetMembers" DATASOURCE="irt"> SELECT ID, Name, Age FROM Members </CFQUERY>
If you only have the above 3 fields (ID, Name and Age) in your database it won't help you much, but if you have other fields (or you consider creating more fields), there is no need for you to select all data, as is done by using SELECT *. When you just need ID, Name and Age, it is much better and faster, to explicity specify the fields that you require.
Another idea, could to be alphabetize the list after name. This is done by adding a line to the CFQUERY code, so it looks like this:
<CFQUERY NAME="GetMembers" DATASOURCE="irt"> SELECT ID, Name, Age FROM Members ORDER BY Name </CFQUERY>
If you now re-run members.cfm all the output members will be displayed alphabetized by name.
To make things even more fun, why not try to alphabetize all the members backwards, starting with Z and ending with A....
This is again done, by changing the CFQUERY code - to the following:
<CFQUERY NAME="GetMembers" DATASOURCE="irt"> SELECT ID, Name, Age FROM Members ORDER BY Name DESC </CFQUERY>
Last, but not least, you might not want all members displayed on the page (might not be fun, if you have 1000+ members), so to only format 10, change the CFOUTPUT tag from:
<CFOUTPUT QUERY="GetMembers" MAXROWS="10">
Now, you'll only end up with 10 members on members.cfm......
In Cold Fusion you are basically only limited by your imagination. Just to mention some of the interesting possibilites in CFML:
and much much more.
Please feel free to use the Provide Feedback on this Article form below, if you have any comments/questions or suggestions.
Cold Fusion can be downloaded from: http://www.allaire.com
Cold Fusion 4.0 Hosting Showcase: http://www.hostindex.com/coldfusion.shtm
Allaire has a very good Cold Fusion discussion forum at http://forums.allaire.com