CFScript example
The example in this section uses the following CFScript features:
•
Variable assignment
•
Function calls
•
For loops
•
If-else statements
•
WriteOutput
•
Switch statements
The example uses CFScript without any other ColdFusion tags. It creates a structure of course
applicants. This structure contains two arrays; the first has accepted students, the second has
rejected students. The script also creates a structure with rejection reasons for some (but not all)
rejected students. It then displays the accepted applicants followed by the rejected students and
their rejection reasons.
<html>
<head>
<title>CFScript Example</title>
</head>
<body>
<cfscript>
//Set the variables
acceptedApplicants[1] = "Cora Cardozo";
acceptedApplicants[2] = "Betty Bethone";
acceptedApplicants[3] = "Albert Albertson";
rejectedApplicants[1] = "Erma Erp";
rejectedApplicants[2] = "David Dalhousie";
rejectedApplicants[3] = "Franny Farkle";
applicants.accepted=acceptedApplicants;
applicants.rejected=rejectedApplicants;
rejectCode=StructNew();
rejectCode["David Dalhousie"] = "score";
rejectCode["Franny Farkle"] = "too late";
//Sort and display accepted applicants
ArraySort(applicants.accepted,"text","asc");
WriteOutput("The following applicants were accepted:<hr>");
for (j=1;j lte ArrayLen(applicants.accepted);j=j+1) {
WriteOutput(applicants.accepted[j] & "<br>");
}
WriteOutput("<br>");
//sort and display rejected applicants with reasons information
ArraySort(applicants.rejected,"text","asc");
WriteOutput("The following applicants were rejected:<hr>");
for (j=1;j lte ArrayLen(applicants.rejected);j=j+1) {
applicant=applicants.rejected[j];
WriteOutput(applicant & "<br>");
if (StructKeyExists(rejectCode,applicant)) {
switch(rejectCode[applicant]) {
case "score":
140
Chapter 6: Extending ColdFusion Pages with CFML Scripting
functions
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers