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

----- Original Message -----
From: "David Berry" <[EMAIL PROTECTED]>
To: "CF-Talk" <[EMAIL PROTECTED]>
Sent: Wednesday, February 25, 2004 9:26 AM
Subject: Proper CF Insert of an array

> 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!
>
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to