Build Master/Detail page in ColdFusion
by Omar Elbaga
Build a dynamic Master/Detail Page in ColdFusion. Tutorial assumes no prior knowledge of ColdFusion. Happy programming! UDnewbie.com


ColdFusion Master/Detail Pages

Page 1, Page 2, Page 3

After experimenting with some ColdFusion, I found that it is an excellent Web language and probably the most efficient. I say efficient because the definition is:

efficient - Acting or producing effectively with a minimum of waste, expense, or unnecessary effort. Exhibiting a high ratio of output to input.

And that's exactly what makes this language awesome. if ColdFusion does not fit that definition, honestly I dont know what does. I just wanted to share what I had time to share to the Ultradev community regarding ColdFusion. There are plenty of CF resources available online if you plan to really get into ColdFusion.

This tutorial will demonstrate a pair of dynamic Master/Detail Pages and how simple and easy something like this is using ColdFusion. Inevitably, it will also demonstrate how to dynamically display records from a database. You might be looking to see if this tutorial is long, but believe me, it could have been done in about 3 paragraphs, but I overly exaggerated the content to make it as simple as possible. If you don't believe me, look at the final code of both of these pages and see for yourself all that you will be typing. Tutorial assumes no experience with ColdFusion at all. Lets get started.

Prior to jumping in, I want to introduce you to a few tags that will be used so that you arent just copying and typing without understanding.

1. <cfoutput> used to output ColdFusion variables, more on this below...

2. <cfquery> This tag does a whole lot, but in our tutorial, it ultimately opens a Recordset for us. This tag and the SQL is all you need to open a Recordset.

3. #URL.CatID#
holds a variable called CatID passed in a queryString. This is synomous to ASP's Request.QueryString("CatID"). We will be using this to pass variables through a QueryString and retrieve it on the next page.

4. Variables have to be surrounded by #. So if we have a variable called CatID, ColdFusion can only read it if it is written as such #CatID#. In addition, variables have to be within outputted with the <cfoutput> tag as mentioned earlier. So in order for ColdFusion to output this variable, it must be typed as such
<cfoutput>#CFvariable#</cfoutput>
isn't that cool!

Upon this, we are off to conquer the World ... well not really but it's ok to dream and anyway ColdFusion will make you feel that way o)

Lesson: Master Page - part 1

Database: You should have a database with 2 tables. The second Table should have a foreign key of the First table's Primary Key. huh? I think i said that backwards. A simple example would be:

download a database to use
tblManufacturers tblProducts
ManufacturerID Primary key ProductID Primary key
Manufacturer Product
  ManufacturerID foreign key
Although we will be using a category/subcategory database.
DSN: Please have a DSN name set up for your database. You can do this locally or on a remote server.

1. Open a new page in UD or in any textEditor. I will be using Notepad just to show you how easy & fun coding ColdFusion is. Well, also because I run win98 on 64 mb RAM and UD will take a long time to open ... just kidding.

2. Save your new page as Master.cfm. The page should have the basic html tags:

If you use Ultradev or Dreamweaver, the html will be all ready for you.
Leave some room above the <html> tag to type your ColdFusion

3. Above the HTML start by typing
<cfquery>


</cfquery>


4. This is the basic tag to open your recordset. We need to simply add our SQL which those of who use Ultradev should know the basic syntax of as we see it all the time. It's that stuff you always see in the Recordset dialog box o).

<cfquery>

SELECT *
FROM tblCat

</cfquery>


5. We need to name our Recordset and call the DSN. Watch how easy. It will be added in the first tag.

<cfquery name="rsMaster" datasource="udnewbie">

SELECT CatID, CatTitle
FROM tblCat

</cfquery>

Name is where you name your Recordset and datasource is where you type the name of your DSN for the database you are using. I named my Recordset "rsMaster" and my DSN is called "udnewbie"

Lesson
: Master Page - part 2

We have just opened our Recordset, we need to display the Records on the page now.
What would the ASP variable for our CatTitle database column?
<%=rsMaster.Fields.Item("CatTitle").Value%>
Well in ColdFusion, it is:
#rsMaster.CatTitle#

6. Type this in between the body tags:

#rsMaster.CatTitle#

7.
Remember what we said earlier about how ColdFusion displays output, we need to use the <cfoutput> tag, so change it to:

<cfoutput>

#rsMaster.CatTitle#

</cfoutput>

8. One last thing we have left to do is tell the output which recordset to take this variable from. This also goes in the first tag:
<cfoutput query="rsMaster">

#rsMaster.CatTitle#

</cfoutput>



9. Look at your page through the web browser. We have just dynamcially displayed the Category Titles from our Database.

Go to Page 2

Omar Elbaga

Starting out as a fine artist, Omar Elbaga gradually moved to computer graphic arts. He was particularly amazed by the power of the World Wide Web, so he embarked upon building small-scale sites for fun utilizing HTML and his Art background. Falling in love with designing web pages and its potential, he began a career in web design. Omar has since been in the web development field for several years. With his head in computer books nearly 24 hours a day, Omar moved on to enhance his skills from web design to web programming.

Most of his work involves building database-driven web sites for small companies. Omar is currently running a popular Dreamweaver MX resource site named dmxfire.com

See All Postings From Omar Elbaga >>

Place your comment about Build Master/Detail page in ColdFusion




You must me logged in to write a comment
Comments

Be the first to place a comment