Skip to main content
Inspiring
May 25, 2010
Question

Problem with looping on some List

  • May 25, 2010
  • 1 reply
  • 673 views

More than 1 records are shown as result of a form search and from my debug screen I'm facing a comma delimited values from each fields as shown below. The requirement is for the user to be able to do an update to each record at once instead of one record at a time.

Users should be able to check on the checkboxes next to Diane, Suzane,Debbie's records and hit the Update button, those record will get updated.

I tried cfloop list, looping through PersonalId list but it did not help when I look at my database I got:

PersonalId = 1232

FirstName = Diane,Suzanne,Debbie

LastName = Smith,Boyle,Reynold

PersonalId = 3433

FirstName = Diane,Suzanne,Debbie

LastName = Smith,Boyle,Reynold

etc

Form Fields:
FirstName = Diane,Suzanne,Debbie,David,Mark,Ron

LastName = Smith,Boyle,Reynold,Tomang,Schulteis,Wong

PersonalId = 1232,3433,4532,234,678,65,77

DeptCode = ACC,FRNTD,DPRD,MRKT,BSNS,ACQUIS

This topic has been closed for replies.

1 reply

ilssac
Inspiring
May 25, 2010

Most of us solve this problem by including an index value in the field names.

Thus you could get a form data structure that could look something like this.

FirstName_1232 = Diane

LastName_1232 = Smith

FirstName_3433 = Suzanne

LastName_3433 = Boyle

Then some relatively easy looping for the form collection and|or form.fieldnames list combined with array notation you can separate this form data into its related records and update the database appropriately.

<cfset id-Test = 1232>

<cfoutput>

  #form['FirstName_' & id]#

  #form['LastName_' & id]#

</cfoutput>

You could just do a list loop like you are doing, but use the listGetAt() function for the FirstName and LastName fields.  But that is relying on the assumption that all the form lists are in the same order which is something I am uncomfortable trusting across all browsers and web server combinations past, present and future.

Inspiring
May 25, 2010

You could just do a list loop like you are doing, but use

the listGetAt() function for the FirstName and LastName

fields. But that is relying on the assumption that all the

form lists are in the same order which is something I am

uncomfortable trusting across all browsers and web server

combinations past, present and future.

Plus, that approach immediately breaks if any of the form field values contain commas. So the recommendation of using an index in the form field names is the right way to go (IMO).