Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Setting Variables and Displaying Their Values

In any programming language, it is common to create named areas in memory called variables and store key values there for reference during the execution of the program. ColdFusion is no different in this regard and enables you to create variables of many types, including

  Numeric
  Strings
  Boolean (TRUE or FALSE)
  Date/Time
  Lists (items delimited by commas or another character of your choosing)
  Arrays
  Queries

Each type of variable is instantiated by the <CFSET> tag. <CFSET> is a standalone tag with the following syntax:

<CFSET variable = value>

Some sample variable assignments might look like

<CFSET sorted = FALSE>
<CFSET total = 0>
<CFSET name = “Allaire”>
<CFSET today = DateFormat(Now(),‘mm/dd/yy’)>
<CFSET flavors = “Vanilla,Chocolate,Strawberry”>


NOTE:  The Now() function fetches the system date and time, and the DateFormat function applies the mm/dd/yy format to the date.

You also use <CFSET> to update the value of an existing variable. For example,

<CFSET total = total + item_cost>

takes the value currently stored in the variable total, adds item_cost to that amount, and then stores the result back in the variable total.

Creating arrays and queries requires a few other ColdFusion functions in addition to the <CFSET> tag because these data structures are more complex than the other variable types. To create an array, you first have to use the ArrayNew() function:

<CFSET planets = ArrayNew(1)>


NOTE:  The number you pass to the ArrayNew() function specifies how many indexes the array is to have. Arrays can have up to three indexes.

Also, ColdFusion array indexes start at the value 1. This is different from languages such as JavaScript, with array indexes beginning at 0.


With the array declared, you can then use <CFSET> to assign values to the array:

<CFSET planets[1] = “Mars”>
<CFSET planets[2] = “Venus”>
<CFSET planets[3] = “Earth”>
...
<CFSET planets[9] = “Pluto”>

Query variables are structured much like a query result set in memory. In fact, after you’ve defined a query variable, you can use it just as you would if it were generated by a call to a database, meaning you can loop over the query variable with <CFLOOP> or output the contents of the query variable using <CFOUTPUT>.

To instantiate the query variable, you use the ColdFusion QueryNew() function:

<CFSET contact_info = QueryNew(“Name,Address,City,State,Zip”)>

The quoted list of values you pass to the QueryNew() function is the column names you want to use in the query. With the query created, you can add empty rows to the query so that you can populate it with data. You do this with the QueryAddRow function. Because you have to call this function from within a tag, you most commonly see QueryAddRow used as follows:

<CFSET temp = QueryAddRow(contact_info,20)>

The number following the name of the query indicates how many blank rows to add to the query. If you don’t specify a value, ColdFusion will assume you just want to insert one row. If successful, the QueryAddRow function returns a value of TRUE, and this is what is assigned to the variable temp. Otherwise, temp ends up being set to FALSE.

Finally, to place values into the individual cells of the query, you use the QuerySetCell function. QuerySetCell is used in a <CFSET> tag in much the same way as QueryAddRow. For example, the code

<CFSET temp = QuerySetCell(contact_info,“City”,”Alexandria”,12)>

places the value “Alexandria” in the City column of the 12th row of the query variable contact_info. If no row number is specified, ColdFusion will insert the value into the last row. If the insert is successful, temp will take on a value of TRUE; otherwise, temp is set to FALSE.

When it comes time to print the value of a ColdFusion variable on an HTML page, you need to know two things:

  You have to use the <CFOUTPUT> tag.
  You have to enclose the name of the variable in pound signs (#) to indicate to ColdFusion that it should replace the variable name with its value.

To illustrate what can happen if you miss one or the other, consider the following code:

<CFSET holiday = “Christmas”>
<CFOUTPUT>holiday</CFOUTPUT><P>
#holiday#<P>
<CFOUTPUT>#holiday#</CFOUTPUT>

When ColdFusion processes this code, it produces the HTML page you see in Figure 34.17. In the first line, you just see the word “holiday” because no pound signs tell ColdFusion to replace the variable with its value. You see #holiday# on the second line because ColdFusion will only do variable value substitution inside of <CFOUTPUT> and </CFOUTPUT> tags. The third line reads Christmas because both the <CFOUTPUT> tag and the pound signs are used.


FIGURE 34.17  Getting ColdFusion to output variable values can be tricky initially.

A useful variant of the <CFOUTPUT> tag is to use its QUERY attribute to make the output loop over an entire query result set. If you have done a query that retrieves students’ names and exam scores, for example, you could print them all out in a table using the following code:

<TABLE BORDER=1>
<TR><TH>Name</TH><TH>Exam Score</TH></TR>
<CFOUTPUT QUERY=“grades”>
<TR><TD>#name#</TD><TD>#score#</TD></TR>
</CFOUTPUT>
</TABLE>


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.