first advice: scope your variables. your <cfset mylist="#groups#">
should be <cfset mylist="#url.groups#">. will save you debugging
headache when you decide to name another var in some other scope 'group'...
cf has a lot of list manipulation functions, and they all are very good.
check out livedocs or cfml reference for more details.
a few things to keep in mind re cf lists:
- default list delimiter is , (comma). if you do not specify a delimiter
in a list function, that's what cf will assume your delimiter to be.
- you can have multiple delimiters in a list.
- cf treats multiple consecutive delimiters as one delimiter - it does
not support empty list elements. i.e. a list like a,b,,,,,c for cf has 3
list items - it considers ,,,,, to be one delimiter.
- in cf8 there is one exception to the above - when you convert list to
array using listtoarray() function, you can tell cf to NOT ignore empty
list elements by setting optional includeEmptyFields function argument
to true
in your particular case your life will be a lot easier if you can make
your Groups url parameter look something like:
Groups=group1:,group2:16,group2:3,group2:17,group3:2,group3:11,group3:18,
....
you now have a comma-delimited list in which each list item is a
:-delimited list! you can easily convert it into your desired array.
if reformatting your list is not possible for some reason, then you are
in for a bit of hoop-jumping:
the code below creates and cfdumps a 2D array 'array2' and a structure
'struct2', as well as outputs values to browser for viewing:
<cfset array2 = arraynew(2)>
<cfset struct2 = structnew()>
<!--- initiate array2 item counter --->
<cfset arrcounter=0>
<!--- process url.groups list --->
<cfoutput>
<cfloop list="#urldecode(url.groups)#" index="iii" delimiters="g">
<!---
looping through list using literal letter 'g' as list delimiter since
there is no delimiter between groups in the list -
will be concatenating 'g' to list items at output to form proper
'groupX' value instead of 'roupX' returned by loop
--->
<cfif listlen(iii, ": ") gt 1>
<cfloop list="#listgetat(iii, 2, ': ')#" index="jjj" delimiters=",">
<!--- append array2 array --->
<cfset arrcounter = arrcounter+1>
<cfset array2[arrcounter][1] = "g"&listfirst(iii, ": ")>
<cfset array2[arrcounter][2] = jjj>
<!--- append struct2 structure --->
<cfset struct2["g"&listfirst(iii, ": ")] = jjj>
<!--- output current values --->
g#listfirst(iii, ": ")#, #jjj#<br />
</cfloop>
</cfif>
</cfloop>
</cfoutput>
<br />
<br />
<cfdump var="#array2#">
<br />
<br />
<cfdump var="#struct2#">
you can loop over the created array or structure to insert data into
your db now.
but... are you sure you can't pass your data in any other way than a url
query string? where's the data coming from?
hth
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Brian Sheridan wrote:
> 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[i] = ListGetAt(mylist, i)>
> <cfoutput>#MyArray[i]#<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.
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to
date
Get the Free Trial
http://ad.doubleclick.net/clk;192386516;25150098;k
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:303719
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4