Coldfusion Array Help I Think :(

Discussion in 'Programming' started by sheridanbman, Apr 17, 2008.

  1. #1
    Im not that familiar with Coldfusion and I'm learning. I have spent alot of time with no luck. Can someone please take a peek at my code and help me arrange this data correctly. Any help much appreciated.


    -------------------------------------------------------------
    The data is set from a JS var and Im passing it from URL

    http://localhost/drag/page2.cfm?Groups=group1:%20group2:%2016,3,17group3:%202,11,18group4:%2010,7group5:%201,8,20group6:%209,15,12group7:%205,6group8:%2014,13group9:%204,19

    Here is what my array looks like
    --------------------------------------------------------------
    <cfset MyArray = ArrayNew(1)>
    <cfset mylist="#groups#">
    <cfloop from="1" to=#ListLen(mylist)# index="i">
    <cfset MyArray = ListGetAt(mylist, i)>
    <cfoutput>#MyArray#<br>
    </cfoutput></cfloop>

    And here are my results:
    -------------------------------------------
    group1: group2: 16
    3
    17group3: 2
    11
    18group4: 10
    7group5: 1
    8
    20group6: 9
    15
    12group7: 5
    6group8: 14
    13group9: 4
    19

    THIS IS HOW I NEED TO OUTPUT... PLEASE HELP ME.
    --------------------------------------------------

    Group, UserID
    group2, 16
    group2, 3
    group2, 17
    group3, 2
    group3, 11
    group3, 18
    group4, 10
    group4, 7
    group5, 1
    group5, 8
    group5, 20
    group6, 9
    group6, 15
    group6, 12
    group7, 5
    group7, 6
    group8, 14
    group8, 13
    group9, 4
    group9, 19

    I need the data in this format so i can place it into a database.
     
    sheridanbman, Apr 17, 2008 IP
  2. dshuck

    dshuck Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For starters, this stuff is unnecessary:
    <cfset MyArray = ArrayNew(1)>
    <cfset mylist="#groups#">
    <cfloop from="1" to=#ListLen(mylist)# index="i">
    <cfset MyArray[i] = ListGetAt(mylist, i)>
    <cfoutput>#MyArray[i]#<br>
    </cfoutput></cfloop>
    Code (markup):
    To convert a list to an array, just do this:
    <cfset MyArray = ListToArray(groups) />
    Code (markup):
    One other thing of note is the extraneous use of "##". Where you are setting "mylist" = "groups" (and I am not sure why! :)), typical syntax would be:
    <cfset mylist = groups />
    Code (markup):
    Why not just talk to groups?

    With those points made, give this a shot:
    
    <cfset GroupArray = ListToArray(groups,"group") />
    
    <cfloop from="1" to="#ArrayLen(GroupArray)#" index="i">
    	<cfif ListLen(GroupArray[i],"%20") GT 1>
    		<cfloop list="#ListGetAt(GroupArray[i],2,"%20")#" index="j">
    			<cfoutput>group#i#, #j#<br/></cfoutput>
    		</cfloop>		
    	</cfif>
    </cfloop>
    
    Code (markup):
     
    dshuck, Apr 18, 2008 IP