Hey Will,

Good to see you thinking dynamically!  

You actually don't need an array or a struct to do this, but I'll
write up a quick way using each.

Without array of structs:

<cfset numberoffields = 5>

<!--- actions --->
<cfif weProcessAFormNow and everythingIsValid>
  <cfloop from="1" to="#numberoffields#" index="i">
    <cffile action="upload" formfield="file#i#"...">
    <cfquery datasource="#dsn#">
       INSERT INTO files (title, filename)
       VALUES ('#form["title" & i]#', '#cffile.serverFile#')
    </cfquery>
  </cfloop>
</cfif>

<!--- form --->
<cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
<cfset filename = "file" & i>
<cfset titlename = "title" & i>
<input type="File" name="#variables.filename#" />
<input type="text" name="#variables.titlename#" />
<br />
</cfloop>

If you needed to break that and store everything in an array of
structs, here's how we could do the action part:

<!--- actions --->
<cfif weProcessAFormNow and everythingIsValid>
  <cfset uploadedFiles = arrayNew(1) />
  <cfloop from="1" to="#numberoffields#" index="i">
    <cfset thisFile = structNew() />
    <cffile action="upload" formfield="file#i#"...">
    <cfset thisFile.title = form["title" & i] />
    <cfset thisFile.file = cffile.serverfile />
    <cfset arrayAppend(uploadedFiles, thisFile) />
  </cfloop>

  <!--- Ok, loop over array to do insert --->
  <cfloop from="1" to="#arrayLen(uploadedFiles)#" index="i">
    <cfquery datasource="#dsn#">
       INSERT INTO files (title, filename)
       VALUES ('#uploadedFiles[i].title#', '#uploadedFiles[i].file#')
    </cfquery>
  </cfloop>
</cfif>


-Joe

On Thu, 13 Jan 2005 20:07:00 -0400, Will Tomlinson <[EMAIL PROTECTED]> wrote:
> I'm starting this thread to continue a discussion on file uploads, arrays, 
> etc..
> 
> I'm reading some on Jeff Peters book, Lists, arrays, and structures, and I'd 
> like to ask you guys what would be the best way to handle my form values, 
> looping them into a table.
> 
> Let's say I had this for a form.
> <cfset numberoffields = 5>
> 
> <cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
> <cfset filename = "file" & i>
> <cfset titlename = "title" & i>
> <input type="File" name="<cfoutput>#variables.filename#</cfoutput>" />
> <input type="text" name="<cfoutput>#variables.titlename#</cfoutput>" />
> <br />
> </cfloop>
> 
> How would I build a flexible array or structure that ArrayAppend(ed) or 
> StructAppend(ed) my items into them. Then I loop over the array or structure 
> and insert the values into my table.
> 
> What's the best method? Joe? dave? Could you provide a simple example?
> 
> Thanks,
> 
> Will
> 
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble 
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:190417
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to