• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Error when looping over list

Explorer ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

Looping over a series of lists created from form fields generates an error when one of the fields on the form has been left blank. The form is a list of event dates, start times and end times. One event can have a number of times.

When the end time is uncertain, we want to leave it blank. However, doing so shortens the list created from #form.endtime# so that the lists don't match and there's an error message:

"Invalid list index 2.
In function ListGetAt(list, index [, delimiters]), the value of index, 2, is not a valid as the first argument (this list has 1 elements). Valid indexes are in the range 1 through the number of elements in the list."

How do you get around this problem? Tx.

TOPICS
Advanced techniques

Views

640

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jun 16, 2007 Jun 16, 2007
yoman,

No need for separate forms. Use a counter to number each group of field names

<input name="EventDateID1" ...>
<input name="EventDate1" ...>

<input name="EventDateID2" ...>
<input name="EventDate2" ...>

Then loop through the form fields on your action page

<cfloop from="1" to="#form.maxCounter#" index="counter">
<cfset EventDateID = form["EventDateID#counter#">
<cfset EventDate = form["EventDate#counter#">
... rest of fields

<cfquery name="UpdateFeatures" datasource="#dsn#">
UPD...

Votes

Translate

Translate
Participant ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

<cfparam> Set a default value if none is passed.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

I understand what you're getting at, but where would I put the cfparam? The form is like the attached. So, if there are two event dates, but only one end time, a simplified version of the lists look like this:

FORM.Event_ID = 200, 200 = TheEvent_ID
FORM.eventDatesID = 365, 366 = List_EventIDs
FORM.endTime = 4:30 = ListOfEndTimes

So, when the loop happens, there is no #ListGetAt(ListEndOfTimes,2)# the second time through and there's an error.

Maybe my basic concept is wrong. Where would I put the cfparam?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

Ok, I thought you had empty lists being passed by the form action. So <cfparam> might have helped you there.

This code I'm pretty sure works, I seem to remember reading that you can loop through scope variables like arrays, so that FORM[index] works like...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

That would make all lists the same length, but I don't think it would put the correct values at the right point in the list. Would it?

Say the 2nd item lacked an end time. The code would pad the list to create this:

EventDateID = 1, 2, 3
EndTime = 11pm, 11pm, (Empty)

But I need the end times to correspond with the EventDateIDs:
EventDateID = 1, 2, 3
EndTime = 11pm, (Empty),11pm

I know the problem can be resolved by making each row of fields be a form, processing the one that needs to be updated and then starting over, but that seems annoyingly inefficient.

Tx.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

yoman,

No need for separate forms. Use a counter to number each group of field names

<input name="EventDateID1" ...>
<input name="EventDate1" ...>

<input name="EventDateID2" ...>
<input name="EventDate2" ...>

Then loop through the form fields on your action page

<cfloop from="1" to="#form.maxCounter#" index="counter">
<cfset EventDateID = form["EventDateID#counter#">
<cfset EventDate = form["EventDate#counter#">
... rest of fields

<cfquery name="UpdateFeatures" datasource="#dsn#">
UPDATE EventDates
SET EventDate = '#EventDate#', ....

WHERE EventDateID = #EventDateID#
</cfquery>
</cfloop>

You should validate the values and/or use cfqueryparam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 16, 2007 Jun 16, 2007

Copy link to clipboard

Copied

Sounds like it will work. Thanks. I'll try it Monday when my brain has recovered enough. 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 17, 2007 Jun 17, 2007

Copy link to clipboard

Copied

LATEST
Close the square brackets in the real code of course ...

<cfset EventDateID = form["EventDateID#counter#"]>
<cfset EventDate = form["EventDate#counter#"]>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation