Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for a better way to update a shopping cart

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
US
Hello,

I am attempting to write a script to update a shopping cart. I have the following code:
--------------------------------------------------------------------------------------------------
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;count&quot;>
<!--- set new quantity --->

<cfset session.MyCart.item = form.item>
<cfset session.MyCart.quantity = form.quantity>
<cfset session.MyCart.price = form.price>
<cfset session.MyCart.sub_total = form.price * form.quantity>
<cfset i = i + 1>
</cfloop>
--------------------------------------------------------------------------------------------------
The problem is that this will update fine if there is only one item in the cart. But when there is two, an error is caused.

Can someone tell me what is wrong with this or suggest a better way to do this?

Any help is greatly appreciated.

Thanks!
 
Well... you're looping through a loop with an index named &quot;count&quot;... and you also appear to be keeping a counter named &quot;i&quot;... but you don't seem to be using either counter anywhere.

I would guess that'd be the first problem.

-Carl
 
Oops. Sorry, I was trying something different when I post this code and didn't change it back. Here's what I have:
-----------------------------------------------------------
<cfset i = 1>
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<!--- set new quantity --->

<cfset session.MyCart.item = form.item>
<cfset session.MyCart.quantity = form.quantity>
<cfset session.MyCart.price = form.price>
<cfset session.MyCart.sub_total = form.price * form.quantity>
<cfset i = i + 1>
</cfloop>
------------------------------------------------------------

Thanks!
 
Wait... I just realized that your &quot;TGML&quot; was getting processed (I wondered why you suddenly switched to italics... until I realized the tek-tips forums were processed your array information).

Your code, I'm guessing, actually looks like:
Code:
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;count&quot;>
<!--- set new quantity --->
<cfset session.MyCart[i].item = form.item>
<cfset session.MyCart[i].quantity = form.quantity>
<cfset session.MyCart[i].price = form.price>
<cfset session.MyCart[i].sub_total = form.price * form.quantity>
<cfset i = i + 1>
</cfloop>

Still not quite sure why you're keeping two counters... but maybe you're hiding something, or I'm being dense.

The problem I see is that, even if the above code did work, every item in your cart would be set to the same item, price, quantity, and sub-total (unless there's some more TGML getting processed).

Can you fill us in on how you set up session.MyCart and what the form looks like that is submitting to this code?
And since we're talking arrays, always use the
Code:
[code]
TGML tags so nothing gets lost.
-Carl
 
Ack... that should be &quot;always use the
Code:
[
Code:
code
Code:
]
tags...&quot;
-Carl
 
Here's the Cart:

------------------------------------------------------------
<script language=&quot;Javascript&quot;>
function JumpToCat(){window.location=&quot;catalog.cfm&quot;;}
function JumpToCheckOut(){window.location=&quot;Checkout.cfm&quot;;}
</script>
<cfinclude template=&quot;cartfunctions.cfm&quot;>
<table align=&quot;left&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;600&quot;>
<tr>
<td rowspan=&quot;3&quot; width=&quot;250&quot; valign=&quot;top&quot;></td>
<td width=&quot;450&quot;>
<table width=&quot;450&quot; border=&quot;0&quot; cellspacing=&quot;0&quot;><tr></tr>
<tr>
<td class=&quot;header&quot; valign=&quot;top&quot;>CaterCart Shopping Cart:</td>
<td align=&quot;right&quot; valign=&quot;bottom&quot;><input type=&quot;button&quot; name=&quot;ClearCart&quot; Value=&quot;Check Out Now&quot; class=&quot;button&quot; onclick=&quot;JumpToCheckOut();&quot;></td>
</tr>
</table>
</td>

</tr>
<tr><td width=&quot;450&quot;><hr size=&quot;1&quot;></td></tr>
<tr>
<td>
<table align=&quot;center&quot; width=&quot;100%&quot; border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; bordercolor=&quot;#d4d4d4&quot;>
<cfif IsDefined(&quot;session.MyCart&quot;)>
<tr bgcolor=&quot;#f2f2f2&quot;>
<td>Item:</td>
<td>Quantity:</td>
<td>Price:</td>
<td>SubTotal:</td>

</tr>
<cfset total=&quot;0&quot;>
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<form action=&quot;&quot; method=&quot;post&quot;>
<cfoutput>
<input type=&quot;hidden&quot; name=&quot;itemindex&quot; value=&quot;#i#&quot;>
<tr><td>#session.MyCart.itemname#<input type=&quot;hidden&quot; name=&quot;item&quot; value=&quot;#session.mycart.itemname#&quot;></td>
<td><input type=&quot;text&quot; name=&quot;quantity&quot; value=&quot;#session.mycart.quantity#&quot; size=&quot;3&quot; maxlength=&quot;3&quot; style=&quot;font: smaller;&quot;></td>
<td>#DollarFormat(session.mycart.price)#<input type=&quot;hidden&quot; name=&quot;price&quot; value=&quot;#session.mycart.price#&quot;></td>
<td align=&quot;right&quot;>#DollarFormat(session.mycart.sub_total)#<input type=&quot;hidden&quot; name=&quot;sub_total&quot; value=&quot;#session.mycart.sub_total#&quot;></td>

</tr>
</cfoutput>
</cfloop>
<cfloop from=&quot;1&quot; to = &quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<cfset variables.Total=#session.mycart.sub_total#+#total#>
</cfloop>
<tr>
<td align=&quot;center&quot;> <font style=&quot;font: smaller;&quot;><input type=&quot;submit&quot; name=&quot;ClearCart&quot; value=&quot;Empty Cart&quot; class=&quot;button&quot;>      <input type=&quot;submit&quot; name=&quot;UpdCart&quot; value=&quot;Update Cart&quot; class=&quot;button&quot;></font></td>
<td>Total</td>
<cfoutput><td colspan=&quot;2&quot; align=&quot;right&quot;>#DollarFormat(variables.total)#</td></cfoutput>
</tr>
</form>
</table>
</td>
</tr>
<cfelse>
<tr>
<td colspan=&quot;2&quot; align=&quot;center&quot;>Your Cart is Currently Empty!</td></tr>
</cfif>
<tr><td colspan=&quot;2&quot; align=&quot;center&quot;><hr size=&quot;1&quot;></td></tr>
<tr><td colspan=&quot;2&quot; align=&quot;center&quot;><font style=&quot;font: smaller;&quot;><input type=&quot;button&quot; name=&quot;ShopMore&quot; value=&quot;Continue Shopping&quot; class=&quot;button&quot; onclick=&quot;JumpToCat();&quot;></font></td></tr>
</table>
------------------------------------------------------------

Adding an Item Code:
------------------------------------------------------------
<cflock scope=&quot;session&quot; type=&quot;EXCLUSIVE&quot; timeout=&quot;10&quot;>
<cfif NOT IsDefined(&quot;session.MyCart&quot;) OR ArrayISEmpty(session.MyCart)>
<cfset session.MyCart = ArrayNew(1)>
</cfif>
<cfset i = ArrayLen(Session.MyCart) + 1>
<cfset session.MyCart = STRUCTNEW()>
<cfset session.MyCart.item = form.item>
<cfset session.MyCart.itemname = form.itemname>
<cfset session.MyCart.quantity = form.quantity>
<cfset session.MyCart.price = form.price>
<cfset session.MyCart.sub_total = form.price * form.quantity>

</cflock>
<cflocation url=&quot;Cart.cfm&quot;>
------------------------------------------------------------

Cart Functions:
------------------------------------------------------------
<!--- Deleting Items in Array ---->
<cfif ParameterExists(form.ClearCart)>
<CFSET ArrayClear(session.MyCart)>
</cfif>
<cfif ParameterExists(form.UpdCart)>

<!--- updating items in array --->
<cfif form.quantity NEQ 0>
<cfset i = 1>
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<!--- set new quantity --->

<cfset session.MyCart.item = form.item>
<cfset session.MyCart.quantity = form.quantity>
<cfset session.MyCart.price = form.price>
<cfset session.MyCart.sub_total = form.price * form.quantity>
<cfset i = i + 1>
</cfloop>
</cfif></cfif>
------------------------------------------------------------
 
Grrr... please surround all code with
Code:
[
code
Code:
]
so we can see all the arrays (your
Code:
[
i
Code:
]
's are telling the tek-tips forum software to display italics instead).
-Carl
 
Here's the Cart:

------------------------------------------------------------
Code:
<script language=&quot;Javascript&quot;>
function JumpToCat(){window.location=&quot;catalog.cfm&quot;;}
function JumpToCheckOut(){window.location=&quot;Checkout.cfm&quot;;}
</script>
<cfinclude template=&quot;cartfunctions.cfm&quot;>
<table align=&quot;left&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;600&quot;>
<tr>
<td rowspan=&quot;3&quot; width=&quot;250&quot; valign=&quot;top&quot;></td>
<td width=&quot;450&quot;>
<table width=&quot;450&quot; border=&quot;0&quot; cellspacing=&quot;0&quot;><tr></tr>
<tr>
<td class=&quot;header&quot; valign=&quot;top&quot;>CaterCart Shopping Cart:</td>
<td align=&quot;right&quot; valign=&quot;bottom&quot;><input type=&quot;button&quot; name=&quot;ClearCart&quot; Value=&quot;Check Out Now&quot; class=&quot;button&quot; onclick=&quot;JumpToCheckOut();&quot;></td>
</tr>
</table>
</td>

</tr>
<tr><td width=&quot;450&quot;><hr size=&quot;1&quot;></td></tr>
<tr>
<td>
<table align=&quot;center&quot; width=&quot;100%&quot; border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; bordercolor=&quot;#d4d4d4&quot;>
<cfif IsDefined(&quot;session.MyCart&quot;)>
<tr bgcolor=&quot;#f2f2f2&quot;>
<td>Item:</td>
<td>Quantity:</td>
<td>Price:</td>
<td>SubTotal:</td>

</tr> 
<cfset total=&quot;0&quot;>
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<form action=&quot;&quot; method=&quot;post&quot;>
<cfoutput>
<input type=&quot;hidden&quot; name=&quot;itemindex&quot; value=&quot;#i#&quot;>
<tr><td>#session.MyCart.itemname#<input type=&quot;hidden&quot; name=&quot;item&quot; value=&quot;#session.mycart.itemname#&quot;></td>
<td><input type=&quot;text&quot; name=&quot;quantity&quot; value=&quot;#session.mycart.quantity#&quot; size=&quot;3&quot; maxlength=&quot;3&quot; style=&quot;font: smaller;&quot;></td>
<td>#DollarFormat(session.mycart.price)#<input type=&quot;hidden&quot; name=&quot;price&quot; value=&quot;#session.mycart.price#&quot;></td>
<td align=&quot;right&quot;>#DollarFormat(session.mycart.sub_total)#<input type=&quot;hidden&quot; name=&quot;sub_total&quot; value=&quot;#session.mycart.sub_total#&quot;></td>

</tr>
</cfoutput>
</cfloop> 
<cfloop from=&quot;1&quot; to = &quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<cfset variables.Total=#session.mycart.sub_total#+#total#>
</cfloop>
<tr>
<td align=&quot;center&quot;> <font style=&quot;font: smaller;&quot;><input type=&quot;submit&quot; name=&quot;ClearCart&quot; value=&quot;Empty Cart&quot; class=&quot;button&quot;>      <input type=&quot;submit&quot; name=&quot;UpdCart&quot; value=&quot;Update Cart&quot; class=&quot;button&quot;></font></td>
<td>Total</td>
<cfoutput><td colspan=&quot;2&quot; align=&quot;right&quot;>#DollarFormat(variables.total)#</td></cfoutput>
</tr>
</form>
</table>
</td>
</tr>
<cfelse>
<tr>
<td colspan=&quot;2&quot; align=&quot;center&quot;>Your Cart is Currently Empty!</td></tr>
</cfif>
<tr><td colspan=&quot;2&quot; align=&quot;center&quot;><hr size=&quot;1&quot;></td></tr>
<tr><td colspan=&quot;2&quot; align=&quot;center&quot;><font style=&quot;font: smaller;&quot;><input type=&quot;button&quot; name=&quot;ShopMore&quot; value=&quot;Continue Shopping&quot; class=&quot;button&quot; onclick=&quot;JumpToCat();&quot;></font></td></tr>
</table>[code]
------------------------------------------------------------

Adding an Item Code:
------------------------------------------------------------
[code]<cflock scope=&quot;session&quot; type=&quot;EXCLUSIVE&quot; timeout=&quot;10&quot;>
<cfif NOT IsDefined(&quot;session.MyCart&quot;) OR ArrayISEmpty(session.MyCart)>
<cfset session.MyCart = ArrayNew(1)>
</cfif>
<cfset i = ArrayLen(Session.MyCart) + 1>
<cfset session.MyCart = STRUCTNEW()>
<cfset session.MyCart.item = form.item>
<cfset session.MyCart.itemname = form.itemname>
<cfset session.MyCart.quantity = form.quantity>
<cfset session.MyCart.price = form.price>
<cfset session.MyCart.sub_total = form.price * form.quantity>

</cflock>
<cflocation url=&quot;Cart.cfm&quot;>[code]
------------------------------------------------------------

Cart Functions:
------------------------------------------------------------
[code]<!--- Deleting Items in Array ---->
<cfif ParameterExists(form.ClearCart)>
<CFSET ArrayClear(session.MyCart)>
</cfif>
<cfif ParameterExists(form.UpdCart)>

<!--- updating items in array --->
<cfif form.quantity NEQ 0>
<cfset i = 1>
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<!--- set new quantity --->

<cfset session.MyCart.item = form.item>
<cfset session.MyCart.quantity = form.quantity>
<cfset session.MyCart.price = form.price>
<cfset session.MyCart.sub_total = form.price * form.quantity>
<cfset i = i + 1>
</cfloop>
</cfif></cfif>[code]
------------------------------------------------------------
 
Well... now I'm more confused then ever. I still don't see any array slots.

But, one thing I see right away is that in your add code, you are first defining session.MyCart as an array:
Code:
<cfset session.MyCart = ArrayNew(1)>

but then, immediately after, you try defining it as a structure:
Code:
<cfset session.MyCart = STRUCTNEW()>

Unless you're actually doing:
Code:
<cfset session.MyCart[i] = STRUCTNEW()>
again... hard to tell without those array slots showing.

You're also not tagging your form properly in the cart page.
You would either want to do:
Code:
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
   <form action=&quot;IreallyShouldHaveApagenameHere!&quot; method=&quot;post&quot;>
   <cfoutput>
     <input type=&quot;hidden&quot; ... 
   </cfoutput>
   </form>
</cfloop>

or
Code:
<form action=&quot;IreallyShouldHaveApagenameHere!&quot; method=&quot;post&quot;>
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
  <cfoutput>
     <input type=&quot;hidden&quot; ... 
  </cfoutput>
</cfloop> 
</form>
depending on what you're actually trying to do.


Finally, you might have better luck assigning values to the structure by using StructInsert rather than the structurename.keyname syntax. Also, you're also always supposed to use ArraySet() to initialize an array after ArrayNew():

Code:
<cfif NOT IsDefined(&quot;session.MyCart&quot;) OR ArrayISEmpty(session.MyCart)>
    <cfset session.MyCart = ArrayNew(1)>
    <cfset temp = ArraySet(session.MyCart, 1,1,&quot;&quot;)>
</cfif>
<cfset i = ArrayLen(Session.MyCart) + 1>

<cfset session.MyCart[i] = STRUCTNEW()>
<cfset temp = StructInsert(session.MyCart[i], &quot;item&quot;, form.item)>
<cfset temp = StructInsert(session.MyCart[i], &quot;itemname&quot;, form.itemname)>
<cfset temp = StructInsert(session.MyCart[i], &quot;quantity&quot;, form.quantity)>
<cfset temp = StructInsert(session.MyCart[i], &quot;price&quot;, form.price)>
<cfset temp = StructInsert(session.MyCart[i], &quot;sub_total&quot;, form.sub_total)>


By the way... you never mentioned what error was actually being produced. Is it a ColdFusion error? Or is it simply that the values aren't getting set in your cart?

-Carl
 
Oh... one other thing you might try. I've always created and filled the struct separately, then did a deep copy of it to the session variable (array). Something like:

Code:
<CFSET tempStruct = StructNew()>
<cfset temp = StructInsert(tempStruct, &quot;item&quot;, form.item)>
<cfset temp = StructInsert(tempStruct, &quot;itemname&quot;, form.itemname)>
<cfset temp = StructInsert(tempStruct, &quot;quantity&quot;, form.quantity)>
<cfset temp = StructInsert(tempStruct, &quot;price&quot;, form.price)>
<cfset temp = StructInsert(tempStruct, &quot;sub_total&quot;, form.sub_total)>

<!--- use Duplicate instead of StructCopy, so it doesn't
      try to reference the original structure (tempStruct) --->
<CFSET temp = ArrayAppend(Session.MyCart, Duplicate(tempStruct))>

<!--- always a good idea to clean up after yourself... particularly with structure --->
<CFSET temp = StructClear(tempStruct)>
-Carl
 
My problem is with updating. I am able to add new items in fine. The error I get is:


Error Diagnostic Information

An error occurred while evaluating the expression:


session.MyCart.subtotal = form.price * form.quantity



Error near line 18, column 24.
--------------------------------------------------------------------------------

Cannot convert 3.5000,3.5000 to number.

Please, check the ColdFusion manual for the allowed conversions between data types


The error occurred while processing an element with a general identifier of (CFSET), occupying document position (18:1) to (18:63).


Date/Time: 02/07/03 13:57:15
Browser: Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)
Remote Address: 10.11.116.113
HTTP Referrer: Query String: CFID=17&CFTOKEN=65914456
 
Yes... that more than likely means that you have more than one &quot;price&quot; field on the form (by HTML standard, if two fields on a form have the same name, their values are concatenated when the form is submitted) ... which goes back to one of the original points I made... you're not tagging the form correctly.

If you tag it like:
Code:
<cfloop from = &quot;1&quot; to =&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
   <form action=&quot;IreallyShouldHaveApagenameHere!&quot; method=&quot;post&quot;>
   <cfoutput>
     <input type=&quot;hidden&quot; ... 
   </cfoutput>
   <INPUT TYPE=&quot;submit&quot; ...>
   </form>
</cfloop>

You're only going to have one price field per form (though you'll have multiple forms on the page, each with it's own submit button)... and, thus, only one value in the form.price variable, etc.


-Carl
 
I understand what you're saying. I originally had the code written that way.

But that forces you to update one item in the cart at a time. I don't want to do that. I want to allow my customers to change quantities of multiple items and then be able to click on &quot;Update&quot; to update all items at once and the costs.
 
You can accomplish that... but to do so you need to understand what's going on behind the scenes. While not yet syntactically correct, your current sample code looks like it's looping for each item in the cart, and displaying several text fields (hidden and otherwise) on each loop. Problem is you're naming them all the same.

So your form wants to come out looking like:
Code:
<form action=&quot;IreallyShouldHaveApagenameHere!&quot; method=&quot;post&quot;>

<table>

<input type=&quot;hidden&quot; name=&quot;itemindex&quot; value=&quot;1&quot;>
<tr><td>Some item <input type=&quot;hidden&quot; name=&quot;item&quot; value=&quot;Some Item&quot;></td>
<td><input type=&quot;text&quot; name=&quot;quantity&quot; value=&quot;3&quot;></td>
<td>$5.00 <input type=&quot;hidden&quot; name=&quot;price&quot; value=&quot;5.00&quot;></td>
<td align=&quot;right&quot;>$15.00<input type=&quot;hidden&quot; name=&quot;sub_total&quot; value=&quot;15.00&quot;></td>
</tr>


<input type=&quot;hidden&quot; name=&quot;itemindex&quot; value=&quot;2&quot;>
<tr><td>Some other item <input type=&quot;hidden&quot; name=&quot;item&quot; value=&quot;Some other item&quot;></td>
<td><input type=&quot;text&quot; name=&quot;quantity&quot; value=&quot;2&quot;></td>
<td>$10.00 <input type=&quot;hidden&quot; name=&quot;price&quot; value=&quot;10.00&quot;></td>
<td align=&quot;right&quot;>$20.00<input type=&quot;hidden&quot; name=&quot;sub_total&quot; value=&quot;20.00&quot;></td>
</tr>
</table>

<input type=&quot;submit&quot; ...>
</form>

Problem is... you now have two fields named &quot;itemindex&quot;, two named &quot;quantity&quot;, two named &quot;price&quot;, and so on.
GET and POST allow for this, but what actually ends up happening is that both the values get posted in a single variable as a comma-delimited list. So, in ColdFusion terms, #FORM.itemIndex# is going to contain &quot;1,2&quot;, #FORM.item# is going to contain &quot;Some item,Some other item&quot;, #FORM.Price# will contain &quot;5.00,10.00&quot;, etc.

So, in order to solve your issue, you would either a) have to figure out how to name all the fields uniquely, or b) have to figure out how to parse the values on your action page. A) is the most reliable, because, believe it or not, the HTML spec does not declare in what order the items should be added to the variable list... so you could end up with #FORM.item# equal to &quot;Some item,Some other item&quot;, while #FORM.Price# equals &quot;10.00,5.00&quot; (prices are in the wrong order). Most browsers follow the &quot;first in&quot; rule... but that can't be trusted 100%. Unfortunately, A) is a bit more complicated.
The way I've done it in the past is something like:

Code:
<form action=&quot;IreallyShouldHaveApagenameHere!&quot;  method=&quot;post&quot;>
<table>
<cfloop from=&quot;1&quot; to=&quot;#ArrayLen(session.MyCart)#&quot; index=&quot;i&quot;>
<cfoutput>
<tr>
<td>#session.MyCart[i].itemname#<input type=&quot;hidden&quot; name=&quot;item_#i#&quot; value=&quot;#session.mycart[i].itemname#&quot;></td>
<td><input type=&quot;text&quot; name=&quot;quantity_#i#&quot; value=&quot;#session.mycart[i].quantity#&quot; size=&quot;3&quot; maxlength=&quot;3&quot; style=&quot;font: smaller;&quot;></td>
<td>#DollarFormat(session.mycart[i].price)#<input type=&quot;hidden&quot; name=&quot;price_#i#&quot; value=&quot;#session.mycart[i].price#&quot;></td>
<td align=&quot;right&quot;>#DollarFormat(session.mycart[i].sub_total)#<input type=&quot;hidden&quot; name=&quot;sub_total_#i#&quot; value=&quot;#session.mycart[i].sub_total#&quot;></td>
</tr>
</cfloop>
</table>
<input type=&quot;hidden&quot; name=&quot;itemQty&quot; value=&quot;#ArrayLen(session.MyCart)#&quot;>
<input type=&quot;submit&quot; ...>

What this does is name the fields &quot;price_1&quot;, &quot;price_2&quot;, &quot;quantity_1&quot;, &quot;quantity_2&quot;, and so on.

Then, the action page can do something like:
Code:
<cfif NOT IsDefined(&quot;session.MyCart&quot;)>
    <cfset session.MyCart = ArrayNew(1)>
    <cfset temp = ArraySet(session.MyCart, 1,1,&quot;&quot;)>
<cfelse>
    <!--- since you'll be recreating the array each time,
          you should clear the existing one --->
    <cfset temp = ArrayClear(session.MyCart)>
</cfif>

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#FORM.itemQty#&quot;>
    <CFSET tempStruct = StructNew()>
    <cfset temp = StructInsert(tempStruct, &quot;item&quot;, form[&quot;item_#i#&quot;])>
<cfset temp = StructInsert(tempStruct, &quot;itemname&quot;, form[&quot;itemname_#i#&quot;])><cfset temp = StructInsert(tempStruct, &quot;quantity&quot;, form[&quot;quantity_#i#&quot;])>
<cfset temp = StructInsert(tempStruct, &quot;price&quot;, form[&quot;price_#i#&quot;])>
<cfset temp = StructInsert(tempStruct, &quot;sub_total&quot;, form[&quot;sub_total_#i#&quot;])>

   <CFSET temp = ArrayAppend(Session.MyCart, Duplicate(tempStruct))>

   <CFSET temp = StructClear(tempStruct)>
</cfloop>

Hope it helps.


-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top