Skip to main content
Participant
March 25, 2012
Answered

looping through a form in coldfusion???

  • March 25, 2012
  • 2 replies
  • 4430 views

I want to submit this form.array into database

array
1  COMPETITOR[0][THRUST]
2 COMPETITOR[0][STRENGT]
3 COMPETITOR[0][WEAKNESS]
4 COMPETITOR[0][EXPRESPONSE]
5 COMPETITOR[0][PROJECTID]
6 COMPETITOR[1][THRUST]
7 COMPETITOR[1][STRENGT]
8 COMPETITOR[1][WEAKNESS]
9 COMPETITOR[1][EXPRESPONSE]
10 COMPETITOR[1][PROJECTID]

In this example i have 2 rows for database

How to create array of structures so i can count number of competitor[] and insert values of thrust,stengt,weakness,expresponse and projectid ? IN Coldfusion ofc.

Thanks in advance

This topic has been closed for replies.
Correct answer BKBK

You first have to create a process for setting the value of the parameters for each row. The following is an example.

Here, competitor is a structure of a structure (has nothing to do with a form). It captures information about the number of rows and column names. I have assumed the columns in the database table will correspond to Thrust, Strengt(do you mean Strength?), ProjectID, etc. and that they are all of type VARCHAR except projectID which is numeric. The example sets the parameter values for a total of 20 rows.

<cfset competitor = structnew()>

<cfloop from="1" to="20" index="row_nr">

<cfset competitor[row_nr] = structnew()>

<cfset competitor[row_nr]["thrust"] = "thrust" & row_nr>

<cfset competitor[row_nr]["strengt"] = "strengt" & row_nr>

<cfset competitor[row_nr]["weakness"] ="weakness" & row_nr>

<cfset competitor[row_nr]["expresponse"] = "expresponse" & row_nr>

<cfset competitor[row_nr]["projectid"] = "projectid" & row_nr> 

</cfloop>

<!---<p>Number of rows: <cfoutput>#structCount(competitor)#</cfoutput></p>--->

<!---<p><cfdump var="#competitor#"></p>--->

<cfloop from="1" to="20" index="row_nr">

<cfquery>

    INSERT INTO table_name (thrust, strengt, weakness, expresponse,projectID)

    VALUES ('#competitor[row_nr]["thrust"]#', '#competitor[row_nr]["strengt"]#', '#competitor[row_nr]["weakness"]#', '#competitor[row_nr]["expresponse"]#', #competitor[row_nr]["projectid"]#)

</cfquery>

</cfloop>

2 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
March 26, 2012

You first have to create a process for setting the value of the parameters for each row. The following is an example.

Here, competitor is a structure of a structure (has nothing to do with a form). It captures information about the number of rows and column names. I have assumed the columns in the database table will correspond to Thrust, Strengt(do you mean Strength?), ProjectID, etc. and that they are all of type VARCHAR except projectID which is numeric. The example sets the parameter values for a total of 20 rows.

<cfset competitor = structnew()>

<cfloop from="1" to="20" index="row_nr">

<cfset competitor[row_nr] = structnew()>

<cfset competitor[row_nr]["thrust"] = "thrust" & row_nr>

<cfset competitor[row_nr]["strengt"] = "strengt" & row_nr>

<cfset competitor[row_nr]["weakness"] ="weakness" & row_nr>

<cfset competitor[row_nr]["expresponse"] = "expresponse" & row_nr>

<cfset competitor[row_nr]["projectid"] = "projectid" & row_nr> 

</cfloop>

<!---<p>Number of rows: <cfoutput>#structCount(competitor)#</cfoutput></p>--->

<!---<p><cfdump var="#competitor#"></p>--->

<cfloop from="1" to="20" index="row_nr">

<cfquery>

    INSERT INTO table_name (thrust, strengt, weakness, expresponse,projectID)

    VALUES ('#competitor[row_nr]["thrust"]#', '#competitor[row_nr]["strengt"]#', '#competitor[row_nr]["weakness"]#', '#competitor[row_nr]["expresponse"]#', #competitor[row_nr]["projectid"]#)

</cfquery>

</cfloop>

Participant
March 26, 2012

Thank you both -==cfSearching==- and BKBK very much  ..yesterday i was trying everything and got this done.

<cfset myform = ListtoArray(FORM.fieldnames) />

<cfset count = Val(ArrayLen(myform)/5) >

<cfset newarray =ArrayNew(1) >

<cfoutput>

<cfloop    index="i" from="1"  to="#count#"  >

<cfset stNew=structNew()>

<cfset stNew["thrust"]=#FORM["COMPETITOR"&"["&i-1&"]"&"[THRUST]"]# >

<cfset stNew["strengt"]=#FORM["COMPETITOR"&"["&i-1&"]"&"[STRENGT]"]# >

<cfset stNew["weakness"]=#FORM["COMPETITOR"&"["&i-1&"]"&"[WEAKNESS]"]# >

<cfset stNew["expresponse"]=#FORM["COMPETITOR"&"["&i-1&"]"&"[EXPRESPONSE]"]# >

<cfset stNew["projectid"]=#FORM["COMPETITOR"&"["&i-1&"]"&"[PROJECTID]"]# >

<cfset arrayAppend(newarray, stNew)>

</cfloop>

</cfoutput>

<cfloop index="x" from="1" to="#ArrayLen(newarray)#" >

    <CFQUERY DATASOURCE="#request.dsn#">

        INSERT INTO ProjectCompetitors (idProject,idCompetitor,strengt,weakness,response)

        VALUES  (<cfqueryparam value="#FORM.idproject#" cfsqltype="cf_sql_integer" >,#Val(newarray.thrust)#,'#newarray.strengt#','#newarray.weakness#','#newarray.expresponse#')

    </CFQUERY>

</cfloop>

Inspiring
March 26, 2012

As CF does not really create arrays, you might consider simpler field names. ie For each set thrust_x, strength_x, ... projectID_x . If you store the total number of sets in a hidden field, you can simply loop once and do the extract and insert at the same time. Also since you are repeating the same sql multiple times, be sure to use cfqueryparam on all fields.

<cfparam name="form.numberOfSets" default="0">

<cfloop from="1" to="form.numberOfSets" index="counter">

      <cfset thrust        = FORM["thrust_"& counter]">

      <cfset strength    = FORM["strength_"& counter]">

      .... other fields

      <cfquery ...>

              INSERT INTO ProjectCompetitors ( idProject,idCompetitor,strength,weakness,response )

              VALUES  (  <cfqueryparam value="#FORM.idproject#" cfsqltype="cf_sql_integer" >
                       ,  <cfqueryparam value="#thrust#" cfsqltype=cf_sql_integer" >

                       , <cfqueryparam value="#strength#" cfsqltype="cf_sql_varchar">

                       , .... other fields

                    )

      </cfquery>

</cfloop>

<cfoutput>

<cfloop    index="i" from="1"  to="#count#"  >

<cfset stNew=structNew()>

<cfset stNew["thrust"]=#FORM["COMPETITOR"&"["&i-1&"]"&"[THRUST]"]# >

As an aside, since you are not displaying the variables, there is no need for cfoutput. You can also skip the extra pound signs around the form variables. CF will evaluate the values of those variables just fine without them.

Inspiring
March 25, 2012

I want to submit this form.array into database

Form fields are submitted as simple values (not arrays). Not unless you are using additional code we cannot see.

Can you post your form code and the structure of your table?