Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 180

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
<cfscript>
function binop(operation, operand1, operand2)
{ return (operation(operand1, operand2)); }
function sum(addend1, addend2)
{ return addend1 + addend2;}
x = binop(sum, 3, 5);
writeoutput(x);
</cfscript>
Handling query results using UDFs
When you call a UDF in the body of a tag that has a
is a query column name passes a single element of the column, not the entire column. Therefore, the function must
manipulate a single query element.
For example, the following code defines a function to combine a single first name and last name to make a full name.
It queries the cfdocexamples database to get the first and last names of all employees, and then it uses a
to loop through the query and call the function on each row in the query.
<cfscript>
function FullName(aFirstName, aLastName)
{ return aFirstName & " " & aLastName; }
</cfscript>
<cfquery name="GetEmployees" datasource="cfdocexamples">
SELECT FirstName, LastName
FROM Employee
</cfquery>
<cfoutput query="GetEmployees">
#FullName(FirstName, LastName)#<br>
</cfoutput>
You generally use functions that manipulate many rows of a query outside tags that loop over queries. Pass the query
to the function and loop over it inside the function. For example, the following function changes text in a query column
to uppercase. It takes a query name as an argument.
function UCaseColumn(myquery, colName) {
var currentRow = 1;
for (; currentRow lte myquery.RecordCount; currentRow = currentRow + 1)
{
myquery[colName][currentRow] = UCase(myquery[colName][currentRow]);
}
Return "";
}
The following code uses a script that calls the
query to uppercase. It then uses
<cfscript>
UCaseColumn(GetEmployees, "LastName");
</cfscript>
<cfoutput query="GetEmployees">
#LastName#<br>
</cfoutput>
attribute, such as a
query
function to convert all the last names in the GetEmployees
UCaseColumn
to loop over the query and display the contents of the column.
cfoutput
Last updated 1/20/2012
tag, any function argument that
cfloop
cfoutput
175
tag

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents