Trying To Create A Basic Shopping Cart

2 views
Skip to first unread message

rai...@ozemail.com.au

unread,
Jun 21, 2010, 10:10:09 AM6/21/10
to cfau...@googlegroups.com, coldfusi...@yahoogroups.com

Hi

 

I am trying my hand at a basic shopping cart

 

I have found an example online that uses the following code

 

<cfparam name="session.cart" default="arrayNew()">

<cfset session.cart = arrayAppend( session.cart, structNew() )>

<cfset thisCartItem = arraylen( session.cart )>

<cfset session.cart[thisCartItem].itemID = form.itemID>

<cfset session.cart[thisCartItem].quantity = form.quantity>

<cfset session.cart[thisCartItem].itemName = form.itemName>

 

 

However the code generates the following error message:

 

Object of type class java.lang.String cannot be used as an array

 

 

If someone would like to advise where the above code is causing the error message I would be grateful

Regards

Claude Raiola
(B.Econ Acc; B.Hot. Mngt)
Samaris Software
Email: in...@SAMARIS.net
Website: www.SAMARIS.net
Mobile: 0414 228 948

 

Dmitry Yakhnov

unread,
Jun 21, 2010, 10:21:40 AM6/21/10
to cfau...@googlegroups.com

Hi Claude,

 

Run this line of code only once to remove your session variable, which is a string:

 

<cfset StructDelete(session,"cart") />

 

And here is the correct code:

 

<cfparam name="session.cart" type="array" default="#ArrayNew(1)#" />

<cfset ArrayAppend(session.cart,StructNew()) />

<cfset thisCartItem = ArrayLen(session.cart) />

<cfset session.cart[thisCartItem].itemID = form.itemID />

<cfset session.cart[thisCartItem].quantity = form.quantity />

<cfset session.cart[thisCartItem].itemName = form.itemName />

 

Best regards,

Dmitry.

 

---

Yakhnov Studio, www.yakhnov.info


--
You received this message because you are subscribed to the Google Groups "cfaussie" group.
To post to this group, send email to cfau...@googlegroups.com.
To unsubscribe from this group, send email to cfaussie+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfaussie?hl=en.

Brett Payne-Rhodes

unread,
Jun 21, 2010, 10:25:00 AM6/21/10
to cfau...@googlegroups.com
Hi Claude,

This works for me...

<cfparam name="session.cart" default="#arrayNew(1)#">
<cfset myCartItem = structNew()>
<cfset myCartItem.itemID = form.itemID>
<cfset myCartItem.quantity = form.quantity>
<cfset myCartItem.itemName = form.itemName>
<cfset result = arrayAppend( session.cart, myCartItem )>


Cheers,

Brett
B)


rai...@ozemail.com.au wrote:
> Hi
>
>
>
> I am trying my hand at a basic shopping cart
>
>
>
> I have found an example online that uses the following code
>
>
>
> <cfparam name="session.cart" default="arrayNew()">
>
> <cfset session.cart = arrayAppend( session.cart, structNew() )>
>
> <cfset thisCartItem = arraylen( session.cart )>
>
> <cfset session.cart[thisCartItem].itemID = form.itemID>
>
> <cfset session.cart[thisCartItem].quantity = form.quantity>
>
> <cfset session.cart[thisCartItem].itemName = form.itemName>
>
>
>
>
>
> However the code generates the following error message:
>
>
>

> *Object of type class java.lang.String cannot be used as an array *


>
>
>
>
>
> If someone would like to advise where the above code is causing the
> error message I would be grateful
>
> Regards
>
> Claude Raiola (B.Econ Acc; B.Hot. Mngt)
> Samaris Software

> Email: in...@SAMARIS.net <mailto:in...@TrackingCentral.om.au>
> Website: www.SAMARIS.net <http://www.TrackingCentral.com.au>


> Mobile: 0414 228 948
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "cfaussie" group.
> To post to this group, send email to cfau...@googlegroups.com.
> To unsubscribe from this group, send email to
> cfaussie+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cfaussie?hl=en.

--
Brett Payne-Rhodes
YourSite Web Solutions
w: http://www.yoursite.net.au
e: br...@ehc.net.au
t: +61 (0)8 9371-0471
m: +61 (0)414 371 047

Peter Tilbrook

unread,
Jun 21, 2010, 11:45:51 AM6/21/10
to cfau...@googlegroups.com
Yes as a "complex" type is always better to declare an array, and the housework that goes with it.

Peter Tilbrook
Managing Director, ColdGen Internet Solutions
Professional Adobe ColdFusion 9 Application Development
President, ACT and Region ColdFusion Users Group
PO Box 2247
Queanbeyan, NSW, 2620
AUSTRALIA

Tel: +61-2-6284-2727

Email Address: pe...@coldgen.com
WWW: http://www.coldgen.com/

ABN: 80 826 226 128

Steve Onnis

unread,
Jun 23, 2010, 3:35:41 AM6/23/10
to cfau...@googlegroups.com

Claude

 

1)      ArrayNew() takes 1 argument being the dimension of the array so you need to say ArrayNew(1)

2)      You are setting session.cart to the string of “arrayNew()” so you cant append the cart item because arrayNew(0 is a string, not an array so you need

<cfparam name=”session.cart”, default=”#Arraynew(1)# />

3)      If your cart item keys are the same as your form fields you can just do
<cfset arrayAppend(session.cart, duplicate(FORM)) />

Just saves on code but if you  have lots of fields in your form you don’t need then maybe setting them manually as you have done may be better.

 

 

Steve

 

From: rai...@ozemail.com.au [mailto:rai...@ozemail.com.au]
Sent: Monday, 21 June 2010 11:40 PM
To: cfau...@googlegroups.com; coldfusi...@yahoogroups.com
Subject: [cfaussie] Trying To Create A Basic Shopping Cart

 

Hi

--

Mike Kear

unread,
Jun 23, 2010, 4:50:01 AM6/23/10
to cfau...@googlegroups.com
A really good lesson on creating shopping carts is found in Ben Forta's book ColdFusion Web Application Construction Kit (better known as CFWACK)   or at least it was in the last edition I read.  I'm assuming he still has it in there. 

It covers the basics of creating a shopping cart,  how to do the manipulations you're going to want to do  - add an item, change quantities,  delete an item,  checkout  etc.  And also some other things you might want to add to your shopping cart to make it better for your application.    Since reading that a few years ago, I've been quite confident in custom building shopping carts for a number of sites, knowing I have all the principles covered and including some nice enhancements that suited the particular sites I was building for.

You can pay $300 upwards for a ready-made shopping cart,  but for less than that, and a few hours learning, you can get Ben's book, read up on shopping carts, and be equipped to make your own any time it's needed.

The actual shopping carts are pretty much the same.  The differences between one commercial shopping cart and another are largely in the backend admin - managing orders,  fulfilling orders and creating invoices, doing reports,  that kind of thing.   The actual process of letting users navigate around your site and pop goods into their cart is fairly straightforward and nearly always the same from one site to another.


Cheers
Mike Kear
Windsor, NSW, Australia
Adobe Certified Advanced ColdFusion Developer
AFP Webworks
http://afpwebworks.com
ColdFusion 9 Enterprise, PHP, ASP, ASP.NET hosting from AUD$15/month


--

Reply all
Reply to author
Forward
0 new messages