Search code examples
coldfusionstructurecoldfusion-9

ColdFusion structure nested elements?


I would like to set nested elements inside of the structure. Here is example of my current code:

<cfset fnResults = StructNew()>
<cfset dateList = "HD_DATE1,HD_DATE2,HD_DATE3,HD_DATE4" />
<cfset servicesEquipment = {
    1="Strongly Agree",
    2="Agree",
    3="Don't Know",
    4="Disagree",
    5="Strongly Disagree"
}>
<cfset isActive = {
    1="Yes ",
    0="No "
}>

<cfquery name="UserInfo" datasource="TestDB">
    SELECT TOP 1
        hd_yn1,
        hd_active,
        hd_date1,
        hd_status,
        hd_age1,
        hd_date2,
        hd_age2,
        hd_date3,
        hd_date4,
        hd_age3,
        hd_deg1,
        hd_deg2,
        hd_toner,
        hd_cfgr,
        hd_deg4,
        hd_deg5,
        hd_tonel,
        hd_cfgl,
        hd_hri,
        hd_comment,
        hd_rear,
        hd_lear,
        hd_tosound,
        LTRIM(RTRIM(si_last)) + ', ' + LTRIM(RTRIM(si_first)) AS hd_staff,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_YN1' AND tm_code = hd_yn1) AS zhd_yn1,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_STATUS' AND tm_code = hd_status) AS zhd_status,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_AGE' AND tm_code = hd_age1) AS zhd_age1,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_AGE' AND tm_code = hd_age2) AS zhd_age2,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_AGE' AND tm_code = hd_age3) AS zhd_age3,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_IND' AND tm_code = hd_hri) AS zhd_hri,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_LOSS' AND tm_code = hd_deg1) AS zhd_deg1,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_LOSS' AND tm_code = hd_deg4) AS zhd_deg4,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_TYPE' AND tm_code = hd_deg2) AS zhd_deg2,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_TYPE' AND tm_code = hd_deg5) AS zhd_deg5,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_TONE' AND tm_code = hd_toner) AS zhd_toner,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_TONE' AND tm_code = hd_tonel) AS zhd_tonel,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_CFG' AND tm_code = hd_cfgr) AS zhd_cfgr,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_CFG' AND tm_code = hd_cfgl) AS zhd_cfgl
    FROM userRec WITH (NOLOCK)
        LEFT OUTER JOIN staffInfo
            ON si_staff = hd_staff
    WHERE hd_userid = '10051989'
</cfquery>

<cfset fnResults.recordcount = UserInfo.recordcount>

<cfif UserInfo.recordcount EQ 0>
    <cfset fnResults.message = "No records were found.">
<cfelse>
    <cfloop query="UserInfo">
        <cfset qryRecs = StructNew()>
        <cfloop array="#UserInfo.getColumnList()#" index="columnName">
            <cfif listContains(dateList, columnName, ",")>
                <cfset qryRecs[columnName] = URLEncodedFormat(Trim(DateFormat(UserInfo[columnName][CurrentRow],'mm/dd/yyyy')))>
            <cfelseif columnName EQ 'hd_active'>
                <cfset qryRecs[columnName] = URLEncodedFormat((structKeyExists(isActive, LossInfo[columnName][CurrentRow]))? isActive[UserInfo[columnName][CurrentRow]]:"No ")>
            <cfelseif columnName EQ 'hd_tosound'>
                <cfset qryRecs[columnName] = URLEncodedFormat((structKeyExists(servicesEquipment, UserInfo[columnName][CurrentRow]))? servicesEquipment[UserInfo[columnName][CurrentRow]]:"")>
            <cfelse>
                <cfset qryRecs[columnName] = URLEncodedFormat(Trim(UserInfo[columnName][CurrentRow]))>
            </cfif>
        </cfloop>
    </cfloop>

    <cfset fnResults.data = qryRecs>
</cfif>

<cfdump var="#fnResults#">

Code above use some logic to manipulate the data. Here is example of my output after I dump fncResults:

DATA    
struct
HD_ACTIVE   No%20
HD_AGE1     [empty string]
HD_AGE2     [empty string]
HD_AGE3     36
HD_CFGL     MMO
HD_CFGR     MMO
HD_COMMENT  Test
HD_DATE1    09%2F22%2F1993
HD_DATE2    [empty string]
HD_DATE3    [empty string]
HD_DATE4    [empty string]
HD_DEG1     II
HD_DEG2     MM
HD_DEG4     MM
HD_DEG5     UU
HD_HRI  NN
HD_LEAR     [empty string]
HD_REAR     [empty string]
HD_STAFF    [empty string]
HD_STATUS   PESS
HD_TONEL    ALL
HD_TONER    ALL
HD_TOSOUND  [empty string]
HD_YN1  Y
ZHD_AGE1    [empty string]
ZHD_AGE2    [empty string]
ZHD_AGE3    36
ZHD_CFGL    MIDDLE
ZHD_CFGR    Mild
ZHD_DEG1    Mild
ZHD_DEG2    Unknown
ZHD_DEG4    Mild
ZHD_DEG5    Unknown
ZHD_HRI     None
ZHD_STATUS  Maybe
ZHD_TONEL   All
ZHD_TONER   All
ZHD_YN1     Did Not
RECORDCOUNT     1 

On the front end I have to set all fields that have letter 'z' in front of 'hs' to be my data for title attribute. Because of that I want to organize my structure to look like this:

DATA    
    struct
    HD_ACTIVE   Value: No%20 Title: This is test
    HD_AGE3     Value: 36 Title: Years
    HD_COMMENT  Value: Test Title: Test

As a side note I have tried to set nested variables in my structure. Something like this: <cfset qryRecs[columnName].value =UserInfo[columnName][CurrentRow]> This throw an error:

Element HD_YN1 is undefined in a CFML structure referenced as part of an expression. 

I'm not sure if I need one more structure in order to achive that. Also I'm not sure what is the best way to organize this kind of structure. If you have any suggestions or examples please let me know . Thank you!


Solution

  • This code snippet is not a complete answer but should help you in the nesting structs with the Value/Title keys.

    <!--- in order to nest structs, the 'nesting' parent structs must be created before assigning values to them --->
    <cfif NOT structKeyExists(qryRecs, "columnName")>
        <cfset qryRecs[columnName] = structNew()>
    </cfif>
    <!--- qryRecs[columnName] now exists as a struct so 'Value', 'Title', etc. keys can be added to it --->
    <cfset qryRecs[columnName].value = UserInfo[columnName][CurrentRow]>