7 Replies Latest reply on Jan 8, 2008 6:26 PM by bigbrain28

    How to use cfloop index value as part of another variable

    bigbrain28
      Hello,

      I have a RATE form that first asks "how many rates will you be adding?" that answer generates a series of form elements based on the answer. You then have a form grid to populate, 3 values for each rate (price, code, name).

      This part works fine. The issue is now I need to INSERT the values into the table, so I need to use the INDEX value of the cfloop to recreate the amount (and names) of the fields in the INSERT statement. How do I escape, or re-create the evaluated form elements ( in which their count is #i# as the loop goes) when I need to evaluate for #i# AND evaluate for #form.sku_i_name# at the same pass?

      I am passing, for example, #form.sku_2_Name# where that might equal "Half Page". The number 2 in that variable was assigned during the form creation by the current index of a cfloop. Since my action (insert) page does not know how many (out of 50 possible) sku's are coming, how do I use the skuCount (another hidden value passed as well) to create a proper INSERT SQL command when I need to eval the form element and the index at the same time?

      Obviously #sku_#i#_Name# does not work, and #sku_i_Name# does not either... I know this can be done, and that I am just doing it wrong.

      Thanks for any help!
        • 1. Re: How to use cfloop index value as part of another   variable
          Level 7
          bigbrain28 wrote:
          > Obviously #sku_#i#_Name# does not work, and #sku_i_Name# does not either... I
          > know this can be done, and that I am just doing it wrong.

          Yup.

          Array Notation. Learn it, love it. It is a very powerful technique to
          access all kind of data structures in Cold Fusion.

          #form["sku_" & i & "_Name"]#
          • 3. Re: How to use cfloop index value as part of another variable
            -==cfSearching==- Level 4
            You just need to use the form structure

            <cfloop index="i" ...>
            <cfset skuName = form["sku_"& i &"_Name"]>
            ...
            </cfloop>
            • 4. How to use cfloop index value as part of another variable
              bigbrain28 Level 1
              Thank You, cfsearching! (and Ian- Sorry, didn't mean to forget you) I don't quite understand 100% how it works, but it works great! I was able to manipulate your code to evaluate all my loop valued variables and dynamically write my INSERT statement as needed. You are a true gem.
              • 5. Re: How to use cfloop index value as part of another   variable
                Level 7
                bigbrain28 wrote:
                > Thank You, cfsearching! I don't quite understand 100% how it works

                As all three of us indicated you use array notation for this type of
                requirement. In ColdFusion you can use array notation or dot notation
                to access structure data. Almost all data in ColdFusion is now some
                type of structure.

                I.E. #Form['myField']# is the same as #Form.myField#.

                The advantage is that with array notation you can do things you can not
                do with dot notation. You can reference elements that have keys that
                are illegal variable names such as one with a space.

                I.E. #url['my field'] is allowed, #url.my field# would fail or course.

                And you can concatenate the key string as we did with your example.

                I.E. #Form['aString' & aVariable]#

                There are many powerful things one can do with this knowledge. One can
                access record sets with array notations. This can allow for very
                sophisticated parsing of a record set.

                I.E. #myQuery['column'][row]#

                You can dynamical call a variable. Thus creating dynamic code that does
                not even need to know what variables exist until run time.

                I.E. #variables[anyName]#

                This just scratches the surface.



                • 6. Re: How to use cfloop index value as part of another variable
                  -==cfSearching==- Level 4
                  It appears everyone posted at about the same time. As the newsgroup poster said, FORM is a structure. You may use array notation to access the form field elements by name like this

                  #form["theFormFieldName"]#

                  What is happening in the code is that inside each iteration of the loop, the variable #i# is evaluated. Which means the field name being evaluated also changes on each loop. So this

                  <cfset skuName = form["sku_"& i &"_Name"]>

                  Gets translated to ...

                  <cfset skuName = form["sku_1_Name"]> first loop
                  <cfset skuName = form["sku_2_Name"]> second loop
                  <cfset skuName = form["sku_3_Name"]> third loop
                  ... etcetera

                  Until the loop hits #form.skuCount#. Make sense?
                  • 7. Re: How to use cfloop index value as part of another variable
                    bigbrain28 Level 1
                    I somehow made it the last 4yrs never knowing about this. Weird how certain things just never come up. I did manage to grasp the concept after a bit of re-use, I even used it to repopulate the dropdown;

                    <select name="select" class="INPUTBOX" id="select">
                    <cfloop from="1" to="#skuCount#" index="i">
                    <cfset skuName = rates["sku_"& i &"_Name"]>
                    <option value="#skuName#">#skuName#</option>
                    </cfloop>
                    </select>

                    where "rates" is the name of the query. This new knowledge will open alot of array based & loop iteration based logic for me, thank you all!

                    P.S. I decided to add a field called 'skuCount' in the table so my output loop would has a defined value to stop at, otherwise it would involve reading and ignoring all 50 (other) potentially null fields, that makes sense, right?