CFSCRIPT 基本陳述式

這段是給我自己常常忘記如何使用的註記:
ColdFusion 的標準 tag 語法法以外,還有 script 語法,
下面是基本的 script 語法使用方式:
script語法是包在一個 <cfscript> ... </cfscript> 之間的
每一行指令要用semicolon ; (分號)結尾,但備註與{大括號}結尾不用加
註解方式
//這是單一行的註解
mojo = 1; //這是放在指令後面的註解
/*
    這是多行的
    註解
*/


設定變數
foo = "foo bar";



IF / Else IF / Else判斷式
if (fruit IS "apple"){
        WriteOutput("I like Apples");
}
else if (fruit IS "orange"){
        WriteOutput("I like oranges");
}
esle {
        WriteOutput("I don't like fruit");
}

FOR Loop 迴圈
for (i=1;i LTE ArrayLen(array);i=i+1) {
        WriteOutput(array[i]);
}

FOR IN Loop 迴圈
struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
        WriteOutput(key);
}

While Loop 迴圈
x = 0;
while (x LT 5) {
        x = x + 1;
        WriteOutput(x);
}

//OUTPUTS 12345

Do / While Loop 迴圈
x = 0;
do {
        x = x+1;
        WriteOutput(x);
} while (x LTE 0);

// OUTPUTS 1

Switch Case 條件選擇
switch(fruit) {
    case "apple":
        WriteOutput("I like Apples");
        break;
    case "orange":
        WriteOutput("I like Oranges");
        break;
    default:
        WriteOutput("I like fruit");
}

Try / Catch Statements 分析
try {
    x = 8 / 0;
}
catch(Any e) {
    WriteOutput("Error: " & e.message);
}

User Defined Function (UDF)自訂函數
function funky(mojo) {
        var dojo = 0;
        if (arguments.mojo EQ dojo) {
                return "mojo";
        }
        else { return "no-mojo"; }
}

建立/使用 Query 查詢(用dump顯示)
getList = new Query();
getList.setDataSource('myConn');
getList.setSQL("SELECT *
    FROM empl
    WHERE empl_id = '#empid#'
    ");
results = getList.Execute(); //執行
//取得執行語法
writeDump(var = getList.getPrefix() , label = "out put getList's SQL");
//取得筆數
writeDump(var = getList.getResult().recordcount , true);
//頃印資料
writeDump(var = getList.getResult() , false);


留言

這個網誌中的熱門文章

【研究】列印的條碼為什麼很難刷(掃描)

C# 使用 Process.Start 執行外部程式

統一發票列印小程式