Skip to main content
Inspiring
June 16, 2008
Answered

Assemble and Insert

  • June 16, 2008
  • 2 replies
  • 244 views
This may be a lot simpler than I think, but I am drawing a blank. I am attempting to build a timesheet when an employee can report their time off per month. Each row consists of a date 2 text field and 2 selects (2 for hours off and 2 for types).

I need to assemble the rows at the end and insert them into the database so it looks like:

date | hoursoff1 | offtype1 | hoursoff2 | offtype2 | uniqname

I am using a loop based on DaysInMonth to generate the form elements. Any ideas on how I can look and insert each of the rows? I was going to name each form element based on the date, but not sure how to assemble them after for insert. Has to be a better way.

Here is my code so far:

<cfloop from="0" to="#daysInMonth(Now())#" index="i">
<cfset i = i +1>
<table width="400" border="0">
<tr>
<td width="20%"><cfoutput>#thisMonth#/<cfif i LTE 9>0</cfif>#i#/#thisYear#</cfoutput></td>

<td width="20%"><input name="<cfoutput>#thisMonth#/<cfif i LTE 9>0</cfif>#i#/#thisYear#-1</cfoutput>" type="text" id="to1" onBlur="showans()" size="4" maxlength="4"/></td>
<td width="20%">
<select name="codeType1" id="codeType1" onBlur="showans()">
<option value="" selected="selected"></option>
<cfoutput query="codetype" onblur="showans()">
<option value="#ID#">#ID#</option>
</cfoutput>
</select></td>
<td width="20%"><input name="<cfoutput>#thisMonth#/<cfif i LTE 9>0</cfif>#i#/#thisYear#-2</cfoutput>" type="text" id="to2" onBlur="showans()" size="4" maxlength="2" /></td>
<td width="20%"><select name="codeType2" id="codeType2">
<option value="" selected="selected"></option>
<cfoutput query="codeType" onblur="showans()">
<option value="#ID#">#ID#</option>
</cfoutput>
</select></td>
</tr>
</table>
</cfloop>
This topic has been closed for replies.
Correct answer wkolcz
Here was my solution.

<cffunction name="insertDays" access="public" returntype="void" output="false" hint="I check for and then insert the dates / time off">
<cfargument name="event" type="ModelGlue.Core.Event">
<cfset recordDAO = createObject("component", "edu.umich.timesheetDAO")>
<cfset uniqname = arguments.event.getValue("uniqname")>
<cfset i = 0>
<cfloop from="0" to="#daysInMonth(Now())#" index="i">
<cfset i=i + 1>
<cfoutput>
<cfset time = arguments.event.getValue('#i#_time')>
<cfset type = arguments.event.getValue('#i#_type')>
<cfset time2 = arguments.event.getValue('#i#_time2')>
<cfset type2 = arguments.event.getValue('#i#_type2')>
<cfif time NEQ "">
<cfset timeoff = "#time#">
<cfset timetype = "#type#">
<cfset toDate = "#this.MM#/#i#/#this.YYYY#">
<cfset addRecord = recordDAO.addTODates(#toDate#, #uniqname#, #timeoff#, #timetype#)>
</cfif>
<cfif time2 NEQ "">
<cfset timeoff = "#time2#">
<cfset timetype = "#type2#">
<cfset toDate = "#this.MM#/#i#/#this.YYYY#">
<cfset insertRecord = recordDAO.addTODates(#toDate#, #uniqname#, #timeoff#, #timetype#)>
</cfif>
</cfoutput>
</cfloop>
</cffunction>

2 replies

wkolczAuthorCorrect answer
Inspiring
June 17, 2008
Here was my solution.

<cffunction name="insertDays" access="public" returntype="void" output="false" hint="I check for and then insert the dates / time off">
<cfargument name="event" type="ModelGlue.Core.Event">
<cfset recordDAO = createObject("component", "edu.umich.timesheetDAO")>
<cfset uniqname = arguments.event.getValue("uniqname")>
<cfset i = 0>
<cfloop from="0" to="#daysInMonth(Now())#" index="i">
<cfset i=i + 1>
<cfoutput>
<cfset time = arguments.event.getValue('#i#_time')>
<cfset type = arguments.event.getValue('#i#_type')>
<cfset time2 = arguments.event.getValue('#i#_time2')>
<cfset type2 = arguments.event.getValue('#i#_type2')>
<cfif time NEQ "">
<cfset timeoff = "#time#">
<cfset timetype = "#type#">
<cfset toDate = "#this.MM#/#i#/#this.YYYY#">
<cfset addRecord = recordDAO.addTODates(#toDate#, #uniqname#, #timeoff#, #timetype#)>
</cfif>
<cfif time2 NEQ "">
<cfset timeoff = "#time2#">
<cfset timetype = "#type2#">
<cfset toDate = "#this.MM#/#i#/#this.YYYY#">
<cfset insertRecord = recordDAO.addTODates(#toDate#, #uniqname#, #timeoff#, #timetype#)>
</cfif>
</cfoutput>
</cfloop>
</cffunction>
Inspiring
June 16, 2008
First, normalize your database. The data structure you described is not very useable.

Second, the way you have your loop coded, you will get an extra day that is not in the month.

For the question you actually asked, you can give form fields dynamic names like this.

<input name="constant_part#variable_part#">

and read it on your action page like this
#form["constant_part" && variable_part]#