|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Proper ColdFusion Insert of an array
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 -----
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>
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 -----
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 -----
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!
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!
|
May 24, 2012
|
Latest Fusion Authority Articles
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||