Fallen Angel
Fallen Angel

Reputation: 21

Query to arrays

I have the following query set:

        ID  SERVICEID   DURATION
        1   1           30
        2   1           60
        3   1           90
        4   1           120
        8   2           30
        7   2           60
        6   2           90
        5   2           120
        9   3           30
        10  3           60
        11  3           90
        13  4           30
        12  4           60
        14  5           30
        15  5           60
        16  5           90
        17  6           30
        19  7           30
        18  7           60
        20  8           30
        21  9           30
        22  10          30
        23  11          60
        24  12          60
        25  13          30

I need some sort of a set of arrays like this: ARRAY[SERVICEID] = [DURATION1, DURATION2, DURATION N]

        ARRAY[1] = [30, 60, 90, 120]
        ...
        ARRAY[4] = [30, 60]
        ...
        ARRAY[12] = [60]
        etc..

Upvotes: 0

Views: 264

Answers (1)

Fallen Angel
Fallen Angel

Reputation: 21

I figured it out, using Structure instead of Arrays:

    <cfset durStruct = {} >
    <cfloop query="durations">
      <cfif NOT StructKeyExists(durStruct,serviceid)>
        <cfset durStruct[serviceid] = [] >
      </cfif>
      <cfset ArrayAppend(durStruct[serviceid],duration) > 
    </cfloop>

And then in Javascript:

    <script type="text/javascript" language="JavaScript">
            <cfoutput>let durData = #serializeJson(durStruct)#;</cfoutput>
    </script>

Upvotes: 1

Related Questions