Search code examples
coldfusionstructurecoldfusion-9cfloop

How to build structure with different set of column names?


I have two different set of columns in my cfquery. One set of columns is reserved for the values and second is reserved for title in the form input fields. I'm building structure on the server side and I would like these two in separate columns. Here is example:

<cfquery name="UInfo" datasource="Test">
    SELECT TOP 1
        u_fname,
        u_lname,
        zu_fname,
        zu_lname
    FROM Users WITH (NOLOCK)
    WHERE u_urid = '0012341'
</cfquery>

<cfloop query="UInfo">
    <cfset qryRecs = StructNew()>
    <cfloop array="#UInfo.getColumnNames()#" index="columnName">
        <cfset qryRecs[columnName]['value'] = UInfo[columnName][CurrentRow]>
        <cfset qryRecs[columnName]['title'] = (structKeyExists(UInfo, "z"&columnName)? UInfo[columnName][CurrentRow] : "")>
    </cfloop>
</cfloop>

My code above will produce this kind of output:

U_FNAME     
struct
title   First Time
value   Mark
U_FLAST     
struct
title   New Customer
value   Miller
ZU_FNAME    
struct
title   First Time
value   [empty string]
ZU_LNAME    
struct
title   New Customer
value   [empty string] 

As you can see above my code produced structure for Z columns as well. I don't need separate structure for these columns they should show in TITLE only. My output should be something like this:

U_FNAME     
struct
    title   First Time
    value   Mark
U_FLAST     
struct
    title   New Customer
    value   Miller

I'm not sure how to get desired output. If anyone can help please elt me know. Thank you!


Solution

  • Appears you simply need an if statement on the columnName inside the column names loop

    <cfif left(columnName, 1) eq "u">