Hi Roberto,

If you name your form variables "itemName" and "itemRank" for all the 10 
items, instead of "itemName1" "itemRank1", itemName2 itemRank2 etc., this 
will give you a comma delimited list of values when you submit the form.  So 
form.itemRank would equal something like "1,2,3,4,5,6,7,8,9,10" if all the 
ranks stay the same.  Same for the itemNames, you would have a list of 
comma-delimited values.  Then you could use listToArray to convert the lists 
to arrays.
<cfset itemNamesArray = ListToArray(form.itemName)>
<cfset itemRanksArray = ListToArray(form.itemRank)>

Finally loop over the length of the array and do your inserts:

<cfloop from="1" to="#len(itemNamesArray)#" index="i">
<cfif isDefined("itemNamesArray[i]") AND itemRanksArray[i] NEQ "none">
<cfquery datasource="ranking_dsn">
INSERT INTO rank_tb (itemName, itemRank)
VALUES (#itemName[i]#, #itemRank[i]#)
</cfquery>
</cfif>
</cfloop>

Might not be exactly correct but that's the gist.  This also gives you 
additional flexibility if you go to more than 10 rankings, you don't have to 
change everything.

-- Josh Nathanson


----- Original Message ----- 
From: "Roberto Perez" <[EMAIL PROTECTED]>
To: "CF-Talk" <[email protected]>
Sent: Saturday, February 18, 2006 8:03 AM
Subject: multiple inserts - same fields


> Hi all,
>
> I have a form where users have to provide a ranking for 10 items.
> Each row of the form is a drop-down menu that contains anywhere from
> 5 to 20 items. Each row writes data to the same fields: itemName and
> itemRank. The values of itemName and itemRank come from each separate
> drop-down menu. The value of itemRank is incremented by 1 in each of
> the ten drop-down menues, so that for menu 1 the value of itemRank is
> 1, for menu 2 the value of itemRank is 2, and so on. If some
> positions do not get an item, they get the default value in the
> drop-down menu (which is "none"), and no INSERT is processed for that row.
>
> What I have so far is 10 separate INSERT queries, one for each row, as 
> follows:
>
> <cfif isDefined("FORM.rank1") AND FORM.rank1 NEQ "none">
>   <cfquery datasource="ranking_dsn">
>   INSERT INTO rank_tb (itemName, itemRank)
>   VALUES (#itemName1#, #itemRank1#)
>   </cfquery>
> </cfif>
>
> <cfif isDefined("FORM.rank2") AND FORM.rank2 NEQ "none">
>   <cfquery datasource="ranking_dsn">
>   INSERT INTO rank_tb (itemName, itemRank)
>   VALUES (#itemName2#, #itemRank2#)
>   </cfquery>
> </cfif>
>
> ....and so on.
>
> My question is: what other (more elegant) ways could you think of to
> achieve the same results (i.e., multiple entries to the same table 
> fields)?
>
> I'd appreciate ideas, pointers, and suggestions.
>
> Regards,
>
> Roberto Perez
>
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:232792
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