House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Proper ColdFusion Insert of an array

  << Previous Post |  RSS |  Tree View |  Sort Oldest First |  Subscribe to this Group Next >> 

Proper CF Insert of an array

Thanks so much!  I think I have some rebuilding to do.  my David Berry 02/25/2004 03:32 PM
> From: David Berry Philip Arnold 02/25/2004 12:04 PM
You'll have to loop over single inserts (one row at a time): Jamie Jackson 02/25/2004 11:56 AM
Hi David: Charlie Griefer 02/25/2004 11:50 AM
not sure what you want to do...but yes, you can have cfquery code inbetwixt Tony Weeg 02/25/2004 11:38 AM
Hello, David Berry 02/25/2004 11:33 AM

02/25/2004 03:32 PM
Author: David Berry Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30792#154422 Thanks so much!  I think I have some rebuilding to do.  my array did look strange to me when I ran a cfdump. It was not the way I would have thought it would look like. Thanks to all for the feedback..   ----- Excess quoted text cut - see Original Post for more ----- #</cfoutput> ----- Excess quoted text cut - see Original Post for more -----
02/25/2004 12:04 PM
Author: Philip Arnold Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30792#154383 > From: David Berry > > I am new to structures and arrays, and I need to insert the > results of a cfloop on the array into my database. Can you > insert a cfloop?  Sample array output in below I have > removed the table formating code: That's not a problem You can put a CFQUERY pretty much anywhere in code except for in another CFQUERY (and a few other tags) While within your loop you can just CFQUERY to insert your data <cfloop from="1" to="#ArrayLen(Session.Cart.productID)#" index="ThisItem">    <cfquery datasource="myDSN" name="insert">       insert into MyTable (ProductID, ProductTitle, Qty, Total)       values (          <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Session.Cart.productID[ThisItem]#">,          <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Session.Cart.productTitle[ThisItem]#">,          <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Session.Cart.Qty[ThisItem]#">,          <cfqueryparam cfsqltype="CF_SQL_MONEY" value="#Session.itemTotal#">          )    </cfquery> </cfloop>
02/25/2004 11:56 AM
Author: Jamie Jackson Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30792#154381 You'll have to loop over single inserts (one row at a time): <cfloop from="1" to="#ArrayLen(Session.Cart.productID)#" index="i">   <cfquery ...> INSERT INTO yourTable (col1, col2, col3) VALUES (   <cfqueryparam value="#session.var1[i]#" cfsqltype="CF_SQL_VARCHAR">,   <cfqueryparam value="#session.var2[i]#" cfsqltype="CF_SQL_VARCHAR">,   <cfqueryparam value="#session.var3[i]#" cfsqltype="CF_SQL_VARCHAR"> )   </cfquery> </cfloop> ----- Excess quoted text cut - see Original Post for more -----
02/25/2004 11:50 AM
Author: Charlie Griefer Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30792#154377 Hi David: First thing I'd like to address is the way way way WAY too many <cfoutput> tags :) Every time you use a <cfoutput>, it's like starting a car.  A </cfoutput> is like shutting the car down. So, for every iteration of your loop, you're starting and stopping the car far too many times just to get from point A to point B.  one <cfoutput></cfoutput> surrounding your <cfloop></cfloop> would suffice. As far as your insert, the most straightforward way would be to loop over the array and do an INSERT statement for each item. <cfloop from="1" to="#arrayLen(session.cart.productID)#" index="i">     <cfquery name="addItem" datasource="myDSN">         INSERT INTO myTable (productID, qty)         VALUES (#session.cart.productID[i]#, #session.cart.Qty[i]#)     </cfquery> </cfloop> Also, for what it's worth, I didn't use <cfqueryparam> for the sake of keeping the example simple...but you should always be using <cfqueryparam> with your SQL. And finally...there's probably a better way to do the shopping cart.  Right now it seems that you have 3 arrays (one for ID, one for description, one for qty).  That means if you delete from one, you have to remember to delete from every other.  Not a big deal, but not necessarily a good idea either. You might want to try one array of structures. session.cart = arrayNew(); session.cart[1] = structNew(); session.cart[1].prodID = 1234; session.cart[1].description = "Fuzzy Underwear"; session.cart[1].qty = 745; session.cart[2] = structNew(); session.cart[2].prodID = 4321; session.cart[2].description = "Navel Lint Ball"; session.cart[2].qty = 1; session.cart[3] = structNew(); session.cart[3].prodID = 666; session.cart[3].description = "Preparation HoF"; session.cart[3].qty = 123; now, if you <cfdump var="#session.cart#">, you can see everything consolidated into one variable.  Granted, there are nested complex variables within...but the cart entity itself is one variable.  I find it to be a bit simpler to manage.  I'm a simple guy.  : ) hth, Charlie ----- Excess quoted text cut - see Original Post for more -----
02/25/2004 11:38 AM
Author: Tony Weeg Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30792#154373 not sure what you want to do...but yes, you can have cfquery code inbetwixt cfloops, to perform inserts/updates/deletes based off of values you get from your loop process. tony Hello, First I would like to add to a comment that was made today and show my appreciation for the help that you all give so freely.  I am new to coldfusion and just reading these e- mails have made me a diehard coldfusion fan. Thanks!  now on to my question!! I am new to structures and arrays, and I need to insert the results of a cfloop on the array into my database. Can you insert a cfloop?  Sample array output in below I have removed the table formating code: <cfloop from="1" to="#ArrayLen(Session.Cart.productID)#" index="ThisItem">         <cfoutput>         <cfoutput>#Session.Cart.productID[ThisItem] #</cfoutput>         <cfoutput>#Session.Cart.productTitle[ThisItem] #</cfoutput>         <cfoutput>#Session.Cart.Qty[ThisItem]#</cfoutput>         <cfoutput>#Dollarformat(Session.itemTotal) #</cfoutput>         </cfoutput> </cfloop> Thanks for the assistance!
02/25/2004 11:33 AM
Author: David Berry Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30792#154371 Hello, First I would like to add to a comment that was made today and show my appreciation for the help that you all give so freely.  I am new to coldfusion and just reading these e- mails have made me a diehard coldfusion fan. Thanks!  now on to my question!! I am new to structures and arrays, and I need to insert the results of a cfloop on the array into my database. Can you insert a cfloop?  Sample array output in below I have removed the table formating code: <cfloop from="1" to="#ArrayLen(Session.Cart.productID)#" index="ThisItem">         <cfoutput>         <cfoutput>#Session.Cart.productID[ThisItem] #</cfoutput>         <cfoutput>#Session.Cart.productTitle[ThisItem] #</cfoutput>         <cfoutput>#Session.Cart.Qty[ThisItem]#</cfoutput>         <cfoutput>#Dollarformat(Session.itemTotal) #</cfoutput>         </cfoutput> </cfloop> Thanks for the assistance!
<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

May 24, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
     1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31     

Designer, Developer and mobile workflow conference