BrainStorm MultiMedia is a source for top quality tutorials on Macromedia Dreamweaver MX, Flash MX, Fireworks MX, PHP/MySQL, ActionScript, Photoshop 7, cPanel X, and Cold Fusion MX. Features include How To Rank High In Google and Brainstorm Forums.

Featured Sponsor Of The Month
 

Cold Fusion Web Site Development

I'm a big fan of all things dynamic. I like dynamic music (check out such relative unknowns as King's X, Galactic Cowboys, Life Sex and Death, and 24/7 Spyz) and I like dynamic web content. My personal favorite will always be PHP - gotta expect that, if you know anything about me - but I am also rather fond of the power of Cold Fusion. Although I never touched it before March of 2004, it has quickly grown on me as an extremely powerful language, rivaling the power of PHP. I use Cold Fusion at my work daily, writing dynamic applications and having fun while doing it. The purpose of this tutorial is to familiarize you with some of the basics of Cold Fusion MX (version 6.1) and to explain a few of the more common tags.

An important thing to know about Cold Fusion is that, like PHP, it must have special characters to let the browser know to parse the code. Cold Fusion MX, as with previous versions of Cold Fusion, define this special character as the # sign. It is used in conjunction with CFML to create dynamic or static pages, depending on your preference.

Static Example:
<html>
<head>
<title>
Quick Cold Fusion Example</title>
</head>
<body>

<cfset
var_name = 'nerd'>
<cfoutput>
Brad is a #var_name#</cfoutput>
</body>
</html>

Those familiar with static programming should see no surprises here. In the previous example, a regular HTML page was set out, and then I added the <cfset> and <cfoutput> tags. <cfset> is how you would explicitly declare a variable in Cold Fusion, and <cfoutput> is how you would have that variable printed out on the page.

 

Dynamic Example:
<html>
<head>
<title>
Quick Cold Fusion Example</title>
</head>
<body>
<cfif isdefined("form.submit")>
   <cfquery name="putIt" datasource="data">
     
 INSERT INTO table (firstname) VALUES ('#form.firstname#')
   </cfquery>

   <cfquery name="getIt" datasource="data">
     
 SELECT * FROM table WHERE name = '#form.firstname#'
   </cfquery>
Your first name is: <cfoutput>#getit.firstname#</cfoutput>

</cfif>
<form method="post" action="thispage.cfm">
   Your First Name: <input type="text" name="firstname">
   <input type="submit">
</form>

</body>
</html>

In the above example, it is assumed that you have a database (usually SQL Server, but just about any will work) with a connection string defined inside of Application.cfm. Good time to go over that page really quick - Application.cfm gets invoked at the beginning of every page transition. Whatever variables are in Application.cfm are automatically declared as global by default, so they are live throughout each and every browser session.
You can see that first I wrote <cfif isdefined("form.submit")> before any other code. I like these conditionals at the top of the page. You don't have to like it, that's just how I did it. As with 'regular' programming, the contents of the if statement are going to be executed as long as the condition is true. Everything inside there is the meat and potatoes of the page, really. We'll get to that in a second. Pretend that we just visited this page in your browser. Since the if statement is not satisfied, it is not displayed at all. Instead, the form prints onto the page. You fill in your first name and click on Submit, which takes you to the same page, only this time the if statement is satisfied and it goes through the statements one by one. You can see that the first thing that happens is <cfquery name="putIt" datasource="data">. This is how you define that you want some kind of query. The name attribute is so you can reference it later in variables and such. I'll get to that later. The datasource attribute is the name of a DSN (data source name) resident on the server that connects to a database of some kind. With these 2 pieces of information, the tag runs an INSERT query first, populating the database with your first name. Immediately after this, we hit the database again with a SELECT statement, which draws the data out of the database. Notice that <cfif>, <cfquery>, and <cfoutput> all need to have ending tags, while <cfset> did not. Important side note there. Anyway, we then outputted that information with the <cfoutput> tag by referencing the query and the row with the data that we wanted (#getit.firstname#). Here, the query name is getit and the row name is firstname. This nomenclature is also used elsewhere in Cold Fusion, such as to assign form or CGI variables. For instance, if you were to write <cfset referer = #CGI.HTTP_REFERER#>, that is perfectly legal and would assign #referer# the value that #CGI.HTTP_REFERER# holds.

I hope this short tutorial has helped you get started with Cold Fusion. If there are any problems, feel free to post them to the Forums, and I'll answer.

 

Cold Fusion MX Tags

 


People Online: 68
Members Online: 0

BrainStorm MultiMedia is an internet-based company run by Bradley Beard that provides free tutorials on PHP, MySQL, Dreamweaver MX, Flash MX, ActionScript, Fireworks MX, ColdFusion MX, cPanel X, Photoshop 7, CuteFTP, SQL Server 2000, how to start ranking high in Google, web site promotion and enhancement, and search engine optimization. Along with these tutorials, I also do freelance web design using PHP/MySQL or ColdFusion.

 

Call 321-674-1667 for more information.

Copyright © 2002-2007, Brainstorm Multimedia. All Rights Reserved.