7 Replies Latest reply on Jun 17, 2007 9:30 AM by cf_dev2

    Error when looping over list

    yoman Level 1
      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.

        • 1. Re: Error when looping over list
          Chuck1411
          <cfparam> Set a default value if none is passed.
          • 2. Error when looping over list
            yoman Level 1
            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?
            • 3. Re: Error when looping over list
              Chuck1411 Level 1
              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...
              • 4. Re: Error when looping over list
                yoman Level 1
                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.
                • 5. Re: Error when looping over list
                  cf_dev2 Level 1
                  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
                  • 6. Re: Error when looping over list
                    yoman Level 1
                    Sounds like it will work. Thanks. I'll try it Monday when my brain has recovered enough. ;-)
                    • 7. Re: Error when looping over list
                      cf_dev2 Level 1
                      Close the square brackets in the real code of course ...

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