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

Search cf-talk

July 04, 2009

<<   <   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   

Home /  Groups /  ColdFusion Talk (CF-Talk)

Proper ColdFusion Insert of an array

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Hello,
David Berry
02/25/04 11:33 A
Hi David:
Charlie Griefer
02/25/04 11:50 A
> From: David Berry
Philip Arnold
02/25/04 12:04 P
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
David Berry
02/25/2004 11:33 AM

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!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Weeg
02/25/2004 11:38 AM

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!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Charlie Griefer
02/25/2004 11:50 AM

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 -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jamie Jackson
02/25/2004 11:56 AM

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 -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Philip Arnold
02/25/2004 12:04 PM

> 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>

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
David Berry
02/25/2004 03:32 PM

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 -----


<< Previous Thread Today's Threads Next Thread >>

Mailing Lists