Basically, I have a form that generates a certain number of "types of publications" depending on "departments" input before someone is fil开发者_运维问答ling out this form. (Department specific publication types that are recognized.) There are a couple of fields that go with each publication type...(they are the same fields, so each type will have ...let say 3 fields..) I have a loop that reads this data and puts it into the database. However, if one of the fields are not filled out the value for that particular field is completely skipped thus throwing off the input of data.
Example: User has three fields and these three fields are repeated three times. The user fills out three in the first row two in the second row and three again in the third row. So:
- First field array: 1, 1, 1
- Second field array: 1, 1
- Third field array: 1, 1, 1
I need to figure out a way to mark that empty field in the second field array so it will appear in the list. I could set default values to the fields, but someone could easily delete that data and in name/ title fields it would seem tacky to have "None" or something...
Any Ideas?
Edit: Code Snippet (Note: I cut out all the unimportant style type things...)
<cfoutput query = "getType_PUB">
Publications: #rName# <br />
<input type = "hidden" name = "scholarActivities" value = "#rName#" />
<input type="text" name="inpress09" size = "8"/><br />
<input type="text" name="published09" size = "8"/><br />
<input type="text" name="published08" size = "8"/><br />
<input type="text" name="published07" size = "8"/><br />
</cfoutput>
<cfoutput><input type = "hidden" name = "recordcountPub" value = "#getType_PUB.recordcount#" /></cfoutput>
//////////////DB/////////////
<cfif #form.recordcountPUB# EQ 1>
<cfquery name = "insertSActivities" datasource="cas_evaluation">
INSERT INTO scholar_publications (faculty, scholarActivities, submit09, inpress09, published09, published08, published07)
VALUES ( '#form.name#', '#form.scholarActivities#', '#form.submit09#', '#form.inpress09#', '#form.published09#', '#form.published08#', '#form.published07#')
</cfquery>
<cfelse>
<cfloop from="1" to="#form.recordcountPUB#" index="i">
<cfquery name = "insertSActivities" datasource="cas_evaluation">
INSERT INTO scholar_publications (faculty, scholarActivities, submit09, inpress09, published09, published08, published07)
VALUES ( '#form.name#', '#ListGetAt(form.scholarActivities, i, ',')#', '#ListGetAt(form.submit09, i, ',')#', '#ListGetAt(form.inpress09, i, ',')#', '#ListGetAt(form.published09, i, ',')#', '#ListGetAt(form.published08, i, ',')#', '#ListGetAt(form.published07, i, ',')#')
</cfquery>
</cfloop>
</cfif>
(To elaborate on my comments..) It is safer to create unique field names. Otherwise, your INSERT code may break if the user enters a comma in one of the form fields. Since you are already using a query loop you can append #currentRow# to each set of fields to make the names unique
<cfoutput query = "getType_PUB">
Publications: #rName# <br />
<input name="scholarActivities#CurrentRow#" ... />
<input name="inpress09#CurrentRow#" ... />
<input name="published09#CurrentRow#" ... />
</cfoutput>
<cfoutput>
<input name="recordcountPub" value="#getType_PUB.recordcount#" .. />
</cfoutput>
On your action page, simply loop and extract the values with array notation. No need for the extra CFIF. You can still use CFPARAM or structKeyExists to handle any fields that may not exist (ie checkboxes or radio buttons).
<cfloop from="1" to="#form.recordcountPUB#" index="i">
<!--- extract the values ...--->
<cfset scholarActivities = FORM["scholarActivities"& i]>
<cfset submit09 = FORM["submit09"& i]>
...
<cfquery ....>
Do the INSERT
</cfquery>
</cfloop>
What field types are these? Checkboxes? If so you can set up default values on the action page, with value of 0 or some other value which indicates a non answer. Then check for that non answer value and skip.
Or is the value passed as blank, and your loop code simply not adding it to the list/array?
A quick code example from you make help people give you a better direction.
精彩评论