?
Solved

Shopping Cart

Posted on 2009-02-12
19
Medium Priority
?
1,056 Views
Last Modified: 2012-05-06
I am trying to implement a simply shopping cart. I copied the code below from a Tech Republic's tutorial on creating a shopping cart. I have replace the variable names with those that I am using in my application.  When I run the code I get the error message listed below.

Can you please tell me where I am going wrong, why I am getting the error message and how to resolve the problem.

Thanks in advance



The error message:
Object of type class java.lang.String cannot be used as an array
 
The error occurred in c:\shopping_cart.cfm: line 26

24 : <!--- Append a new element to the array. This element is a new structure. --->
25 : <cfcase value="1">
26 : <cfset session.cart = arrayAppend(session.cart, structNew())>
27 : <!--- Set up a variable to hold the array position that we're inserting into. --->
28 : <cfset thisCartItem = arraylen(session.cart)>

<cfoutput>
<cfparam name="session.cart" default="arrayNew()>">
<cfif isdefined('url.do')>
<cfset ImageID = "#url.ImageID#">
<cfset ImageID = "#url.Title#">
<cfset Price = "#url.Price#">
<cfset Size = "#url.Size#">
<cfset Quantity = "#url.Quantity#">
 
<cfswitch expression="#url.do#">
<!--- Append a new element to the array. This element is a new structure. --->
<cfcase value="1">
<cfset session.cart = arrayAppend(session.cart, structNew())> 
<!--- Set up a variable to hold the array position that we're inserting into. ---> 
<cfset thisCartItem = arraylen(session.cart)> 
<!--- Populate the new structure with the item information passed from the url/form. ---> 
<cfset session.cart[thisCartItem].ImageID = #ImageID#> 
<cfset session.cart[thisCartItem].Title = #Title#>
<cfset session.cart[thisCartItem].Price = #Price#> 
<cfset session.cart[thisCartItem].Size = #Size#>
<cfset session.cart[thisCartItem].Quantity = #Quantity#>
</cfcase>

Open in new window

0
Comment
Question by:wsessoms
  • 12
  • 5
  • 2
19 Comments
 
LVL 29

Expert Comment

by:chilternPC
ID: 23627541
have you tried running it in its original form?
0
 

Author Comment

by:wsessoms
ID: 23628578
No , I will try that and let you know.
0
 

Author Comment

by:wsessoms
ID: 23628680
I tried the code without any changes and got the same error. Below is the code without any changes. I am also attaching the PDF file that I downloaded for their website that contains the code and tutorial I was following.

I must be missing something.
<cfparam name="session.cart" default="arrayNew()">
 
<cfif isdefined('form.act')>
<cfif form.act is "add">
<!--- Append a new element to the array. This element is a new structure. ---> 
<cfset session.cart = arrayAppend( session.cart, structNew() )> 
<!--- Set up a variable to hold the array position that we're inserting into. ---> 
<cfset thisCartItem = arraylen( session.cart )> 
<!--- Populate the new structure with the item information passed from the form. ---> 
<cfset session.cart[thisCartItem].itemID = form.itemID> 
<cfset session.cart[thisCartItem].quantity = form.quantity> 
<cfset session.cart[thisCartItem].itemName = form.itemName>
</cfif>
 
<cfif form.act is "delete">
<!--- Delete the specified item from the cart. ---> 
<cfloop index="thisCartItem" from="1" to="#arrayLen( session.cart )#"> 
<cfif session.cart[thisCartItem].itemID eq form.itemID> 
<cfset session.cart = arrayDeleteAt( session.cart, thisCartItem )> 
</cfif> 
</cfloop>
</cfif>
 
<cfif form.act is "update">
<!--- Update the specified item from the cart. ---> 
<cfloop index="thisCartItem" from="1" to="#arrayLen( session.cart )#"> 
<cfif session.cart[thisCartItem].itemID eq form.itemID> 
<cfset session.cart[thisCartItem].quantity = form.quantity> 
</cfif> 
</cfloop>
</cfif>
 
Done!
</cfif>
 
<form action="testcart.cfm" method="post">
<input type="hidden" name="itemID" value="300" />
<input type="hidden" name="quantity" value="1" />
<input type="hidden" name="itemName" value="Countryside" />
<input type="hidden" name="act" value="add" />
<input type="submit" name="Submit" value="Add to Cart" />
</form>
 
Error Message
 Object of type class java.lang.String cannot be used as an array
 
The error occurred in C:\wwwroot\testcart.cfm: line 14
 
12 : <cfif form.act is "add">
13 : <!--- Append a new element to the array. This element is a new structure. ---> 
14 : <cfset session.cart = arrayAppend( session.cart, structNew() )> 
15 : <!--- Set up a variable to hold the array position that we're inserting into. ---> 
16 : <cfset thisCartItem = arraylen( session.cart )> 

Open in new window

ColdFusionCart.pdf
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:wsessoms
ID: 23629192
Its been awhile since I posted this question. I could really use some help fiquring out what the error means maybe.
0
 
LVL 2

Expert Comment

by:clickitysplit
ID: 23630106
Try changing:

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

to:

<cfset arrayAppend(session.cart, structNew()>
0
 
LVL 2

Accepted Solution

by:
clickitysplit earned 2000 total points
ID: 23630111
sorry:  <cfset arrayAppend(session.cart, structNew())>
0
 

Author Comment

by:wsessoms
ID: 23630133
Shouldn't there be a variable in <cfset>
ex. <cfset variable_name = arrayAppend(session.cart, structNew())>
0
 

Author Comment

by:wsessoms
ID: 23631449
clickitysplit:

I tried the changing the code as you suggested and I still got the same error.
I changed the code to this (see Code Snippet) and it adds the content to the basket but it doesn't append to the array when I try to add another item to the basket...it replaces whats already in the array.

<cfset session.cart = ArrayNew(1)>
<cfset thisitem = StructNew()>

What I think is happening is every time the code runs it's creating a new array and adding the content to the first position instead of appending to the array.

How do you check to see if an array exist and by pass it if it does exist and run the rest of the code?

<!--- Append a new element to the array. This element is a new structure. --->
<cfcase value="1">
<cfoutput>
 
<cfset session.cart = ArrayNew(1)>
<cfset thisitem = StructNew()>
 
#ArrayAppend(session.cart, thisitem)#
 
<!--- Set up a variable to hold the array position that we're inserting into. ---> 
<cfset thisCartItem = #arraylen(session.cart)#> 
<!--- Populate the new structure with the item information passed from the url/form. ---> 
<cfset session.cart[thisCartItem].ImageID = #ImageID#> 
<cfset session.cart[thisCartItem].Title = #Title#>
<cfset session.cart[thisCartItem].Price = #Price#> 
<cfset session.cart[thisCartItem].Size = #Size#>
<cfset session.cart[thisCartItem].Quantity = #Quantity#>
</cfoutput>
</cfcase>

Open in new window

0
 
LVL 29

Expert Comment

by:chilternPC
ID: 23631485
you are correct - you should put that code in an initalisation procedure call that at the beginning of the buyers  session.
0
 

Author Comment

by:wsessoms
ID: 23631515
chilternPC
Can you give me an example.
0
 

Author Comment

by:wsessoms
ID: 23631558
Should I put this into onRequestStart?
0
 

Author Comment

by:wsessoms
ID: 23631746
Up until this point I have been using the Application.cfm page to initialize variables. I know that there is an Application.cfc page that seems to be what is recommended to be used. I am not sure of the difference and/or how to setup an Application.cfc page. Could someone help me to understand setting initailization variables using the Application.cfc page. Do I have to use <cfcomponent>tags? Please give I get more than a one line answer. I really need help with what really seems to me to be a simple problem. I may be wrong but it seems that way to me.

0
 
LVL 29

Expert Comment

by:chilternPC
ID: 23631929
it sounds like you are designing your ecommerce on the fly. do you have an area where the basket is initialised and the session is setup? and also when the session ends (i.e. the shopped may put stuff in the basket but leave the site without buying? does the software handle aborted shopping?

  - for ecommerce I would recommend buying or using tried and tested software (many are free) see
http://www.devscripts.com/category/browse/215/ColdFusion-E-Commerce.html

0
 

Author Comment

by:wsessoms
ID: 23632577
What I have done is created a photography website. I want to implement a simply shopping cart that keeps track of the items being selected for purchase. I have implemented the code to do checkout using PayPal. PayPal is were I am going to keep track of the purchases. I have already setup a mySQL database for image information and file upload to store the images in folders on the server. I don't want a full blown ecommerce package or I would use CFWebstore which I have used in the past.

I am trying to implement a simply solution.

I am going to implement the Application.cfc page to handle onApplicationStart and onSessionStart variables. I think you have helped me in advising me to initialize the variables in the onSessionStart function without actually saying do that. So I have to go to work now. I will keep this thread update and post my progress.  If you can think of a simple solution to accomplish gather the items selected into a basket please post it.

Thanks
0
 
LVL 29

Expert Comment

by:chilternPC
ID: 23633049
oh -  I see - I'll have a think about the interface - have you checked out PayPal's development centre?
https://www.paypal.com/uk/cgi-bin/webscr?cmd=_resource-center
there may be a solution you can use/reuse
0
 

Author Comment

by:wsessoms
ID: 23637737
Yes I have checked out PayPal's development centre and have all of the information to interface to their checkout process. If I send one item at a time then the code that I have would do the trick but I want to be able to gather more than one item into a shopping basket and then send to PayPal. The PayPal end is not a problem it is getting the shopping cart to hold more than one item.
0
 
LVL 29

Expert Comment

by:chilternPC
ID: 23638535
Hmm, I've used Paypal before this way  and simply put their code on each product and paypal kept the basket contents.
sorry can't really help much more - except explain how mine worked - the pages that displayed the product also had paypal code added to allow adding to baskets and checking out. - the variables used to display the product were also then used to put in the paypal code.
I assume you have a page that displays the item that is for sale ?
0
 

Author Comment

by:wsessoms
ID: 23639128
I want the code to be dynamic. I don't want to cut & paste the code for the add to cart button  each product. That is why the original code that I submitted was aimed at appending the items to the shopping cart. Thanks for your suggestion.
0
 

Author Closing Comment

by:wsessoms
ID: 31546343
That line of code helped but the thing that actually pulled the script together required putting the code below on the add_to_cart.cfm page.





After adding that code everything worked. Thanks for your help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question