Evik James
Evik James

Reputation: 10503

CFSCRIPT - How can I change the reference to shorten the name?

I do a for loop in CFSCRIPT. I am looping through a query and dropping the fields into a table cell via a UDF (that's what the wrapCell() is).

for(i = 1; i lte GetUsers.RecordCount; i++) {
Cells = Cells & wrapCell("#GetUsers.FirstName[i]# #GetUsers.LastName[i]#");
Cells = Cells & wrapCell(lcase(GetUsers.Email[i]));
Cells = Cells & wrapCell(getYesNo(GetUsers.Active[i]));
writeOutput(wrapRow(Cells));
}

Each time I need to reference a field I have to write this:

GetUsers.FirstName[i]
GetUsers.LastName[i]
GetUsers.Email[i]
GetUsers.Active[i]

Id' really like to be able to reference this stuff as like this:

FirstName
LastName
Email
Active

Is there a way to change the reference before the output row so that it takes up less space? For example, can I do something like this:

ThisRow = GetUsersStructure.RowInfo[i];
FirstName
LastName
Email
Active

Upvotes: 1

Views: 134

Answers (2)

Bradley Moore
Bradley Moore

Reputation: 748

You can. It'll add overhead.

Convert your row or whole query to a struct. http://www.bennadel.com/blog/149-Ask-Ben-Converting-A-Query-To-A-Struct.htm

Then throw the results into the local scope.

StructAppend( local , QueryToStruct( GetUsers, i ) );

If you're on CF9/railo, then you can access them simply by column name. (I haven't tested this.)

Upvotes: 1

jalpino
jalpino

Reputation: 150

I'm not aware of any native method that would allow you to do this when using <cfscript>, but you could create another convenience method to do it. For example

public struct function getByRow( query q, numeric index ){
  return {
       firstname = q.firstname[index],
       lastname = q.lastname[index],
       email = q.email[index],
       active = q.active[index]
  }; 

}

You could even take it another step further and have the method loop through q.columnlist to make it generic for re-use.

Upvotes: 1

Related Questions