Copy link to clipboard
Copied
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
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
...Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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
</CFQUERY>
</cfloop>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
FirstForce wrote:
<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>
You could of course store structs into an array. You could also use your original idea of form submission. They are all possible solutions.
However, could you please give us an indication what kind of names you want the form fields to have? Will the information for all the competitors be submitted in one form post, as you have suggested here?
Copy link to clipboard
Copied
There are many ways to slice it, but I am curious whether there's a need for such complexity. For example, if that naming convention were required for some ajax feature OR if the structure were being used for something else beyond the insert, that might be a good reason. Otherwise, it seems like an overcomplication.
<cfset myform = ListtoArray(FORM.fieldnames) />
<cfset count = Val(ArrayLen(myform)/5) >
But in either case, I would recommend a different method for counting the number of sets. FORM.fieldnames could easily be thrown off by additional form fields. Using a hidden field to store the counter is more robust IMO.