3 Replies Latest reply on Mar 5, 2008 12:34 PM by masoud_amen

    looping inset

    masoud_amen
      Hi.
      I have this table:
      <CFLOOP query="vendors">
      <tr>
      <td class="tabdata">#vendors.vendor_name#</td>
      <td class="tabdata" ><input type="text" size="8" name="vendorPart_#vendors.vendor_id#" value="#vendors.vendor_code#"></td>
      <td class="tabdata">
      <table width="100%" border="0" cellpadding="2" cellspacing="1">
      <td width="10%" class="tabdata"><input type="radio" name="vendorDefault" value="#vendors.vendor_id#" <CFIF len(trim(vendors.vendor_default)) AND vendors.vendor_default>checked</CFIF>></td>
      <td class="tabdata" width="25%"><input type="text" name="vendorPrice" value="#vendors.vendor_price#" size="8"></td>
      <td class="tabdata" ><input type="text" name="vendorDescription" value="#vendors.vendor_description#" size="52"></td>

      when I wnat to submit muliple values.The loop doesnt work for description and price part.
      here is the insert section code:

      <CFLOOP list="#form.fieldNames#" index="x">
      <CFIF findNoCase("vendorPart_", x)>
      <CFSET vendorID = replaceNoCase(x, "vendorPart_", "")>
      <CFIF len(trim(form[x]))>
      <CFIF vendorID EQ form.vendorDefault>
      <CFSET defaultVendor = 1>
      <CFELSE>
      <CFSET defaultVendor = 0>
      </CFIF>
      <CFQUERY name="addVendorPart" datasource="#session.eManager.datasource#">
      INSERT tbl_product_vendor
      (product_id, vendor_id, vendor_code, vendor_default, vendor_description, vendor_price)
      VALUES
      (#form.productID#, #vendorID#, '#form[x]#', #defaultVendor# , '#vendorDescription#' , #vendorPrice#)
      </CFQUERY>
      </CFIF>
      Plz help.I'm new and I'm really stuck.
      thanks
        • 1. Re: looping inset
          SteveLogan1029
          If I understand what you're doing correctly, this should get you started as one possible solution.

          Include a hidden input with a comma separated list of vendor IDs (use #ValueList(vendors.vendor_id)#)

          <input type="hidden" name="vendorList" value = #ValueList(vendors.vendor_id)#">

          Now each of your form fields should include that ID in them:

          vendorPart_#vendors.vendor_id#
          vendorPrice_#vendors.vendor_id#
          vendorDescription_#vendors.vendor_id#

          Now, when you submit, loop over the hidden input field

          <cfloop list = "#form.vendorList#" index="thisVendor">

          then you can reference the field names using evaluate: #Evaluate("vendorPart_" & thisVendor)#

          Hopefully I understood your question and this is what you were asking.

          Steve
          • 2. Re: looping inset
            Dan Bracuk Level 5
            The first problem is with the loop for your form. You don't appear to be changing your input names each time through. That means each form field will contain a list.

            Try something like this
            <cfloop query="vendors"
            <cfset this_id = vendor_id[currentrow]>
            <input name="some_name#this_id#">
            etc

            Then when you process the form, the numbers at the end of the field names will help you match them.
            • 3. Re: looping inset
              masoud_amen Level 1
              thanks for replying but I've change the first part to this:
              <CFLOOP query="vendors">
              <tr>
              <td class="tabdata">#vendors.vendor_name#</td>
              <td class="tabdata" ><input type="text" size="8" name="vendorPart_#vendors.vendor_id#" value="#vendors.vendor_code#"></td>
              <td class="tabdata">
              <table width="100%" border="0" cellpadding="2" cellspacing="1">
              <td width="10%" class="tabdata"><input type="radio" name="vendorDefault" value="#vendors.vendor_id#" <CFIF len(trim(vendors.vendor_default)) AND vendors.vendor_default>checked</CFIF>></td>
              <td class="tabdata" width="25%"><input type="text" name="vendorPrice_#vendors.vendor_id#" value="#vendors.vendor_price#" size="8"></td>
              <td class="tabdata" ><input type="text" name="vendorDescription_#vendors.vendor_id#" value="#vendors.vendor_description#" size="52"></td>
              </table>
              </CFLOOP>
              and to inserting the informations i've used this code:

              <CFLOOP list="#form.fieldNames#" index="x">
              <CFIF findNoCase("vendorPart_", x)>
              <CFSET vendorID = replaceNoCase(x, "vendorPart_", "")>
              <CFIF findNoCase("vendorPrice_", x)>
              <CFSET vendorPrice = replaceNoCase(x, "vendorPrice_", "")>
              <CFIF findNoCase("vendorDescription_", x)>
              <CFSET vendorDescription = replaceNoCase(x, "vendorDescription_", "")>
              <CFIF len(trim(form[x]))>
              <CFIF vendorID EQ form.vendorDefault>
              <CFSET defaultVendor = 1>
              <CFELSE>
              <CFSET defaultVendor = 0>
              </CFIF>
              <CFQUERY name="addVendorPart" datasource="#session.eManager.datasource#">
              INSERT tbl_product_vendor
              (product_id, vendor_id, vendor_code, vendor_default, vendor_description, vendor_price)
              VALUES
              (#form.productID#, #vendorID#, '#form[x]#', #defaultVendor# , '#vendorDescription#' , #vendorPrice#)
              </CFQUERY>
              </CFIF>
              </CFIF>
              </CFIF>
              </CFLOOP>

              before I add the vendorDescription and vendorPrice field it was working correctly but after I added these 2 field it doesnt insert to DB.I mean the only problem its about vendorDescription and vendorPrice field.I dont know how can I write a code for these 2.
              If I delete them everything works fine!!!
              thanks for yr concern.