Wednesday, November 21, 2012

For-In Loop Over query and struct object

After a long gap now I got some time for my own blog.

Recently I just studied few new functionality which has been added in ColdFusion 10. For-in loop one of the functionality liked.

Lets take a look into the following code to use for-in loop for looping over any structure or any query object.

<cfscript>
    //Create an Query Object
    qryEmployeeList = queryNew("empID, firstName, lastName", "BigInt, VarChar, VarChar");
    queryAddRow(qryEmployeeList, 2);
    querySetCell(qryEmployeeList, "empID", 1, 1);
    querySetCell(qryEmployeeList, "firstName", "Sachin", 1);
    querySetCell(qryEmployeeList, "lastName", "Tendulkar", 1);

    querySetCell(qryEmployeeList, "empID", 2, 2);
    querySetCell(qryEmployeeList, "firstName", "Rahul", 2);
    querySetCell(qryEmployeeList, "lastName", "Dravid", 2);

    //Create Struct
    strEmployeeInfo = structNew();
    strEmployeeInfo['empID'] = "3";
    strEmployeeInfo['firstName'] = "Saurav";
    strEmployeeInfo['lastName'] = "Ganguly";

    //Loop Over Query Object using for in
    for( employee in qryEmployeeList) {
        WriteOutPut(employee.empID & " : " & employee.firstName & " " & employee.lastName & "<br/>");
    }

    //Loop Over struct Object using for in
    for(strfieldName in strEmployeeInfo) {
        WriteOutPut(strfieldName & " : " & structFind(strEmployeeInfo, strfieldName) & "<br/>");
    }
</cfscript>

Happy Coding. Lets wait for some new interesting things in ColdFusion 10.

No comments:

Post a Comment

Followers