2 Replies Latest reply on Jun 17, 2008 12:11 PM by wkolcz

    Assemble and Insert

    wkolcz Level 1
      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>
        • 1. Re: Assemble and Insert
          Dan Bracuk Level 5
          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]#
          • 2. Re: Assemble and Insert
            wkolcz Level 1
            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>