Help with cf array

getting error when adding struNew to arr

Page 1 of 1

8 Replies - 12118 Views - Last Post: 23 April 2007 - 02:46 PM Rate Topic: -----

#1 wiredwizard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 07-May 05

Help with cf array

Posted 07 May 2005 - 12:12 PM

Help please getting the following error:

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

from the following code:


<!--- values being passed from the add to cart form
form.myQty
form.myItem
form.shipping
form.mysku
--->

<!--- set the default for new session --->
<cfparam name="session.cart" default="arrayNew()">

<!--- append new element to array --->
<cfset session.cart = arrayAppend( session.cart, structNew() )>

<!--- set array position --->
<cfset thisCartItem = arrayLen( session.cart )>

<!--- populat the new structure --->
<cfset session.cart[thisCartItem].myItem = form.myItem>
<cfset session.cart[thisCartItem].myQty = form.myQty>
<cfset session.cart[thisCartItem].shipping = form.shipping>
<cfset session.cart[thisCartItem].mysku = form.mysku>

Is This A Good Question/Topic? 0
  • +

Replies To: Help with cf array

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Help with cf array

Posted 07 May 2005 - 12:28 PM

Which line is the error on?
Was This Post Helpful? 0
  • +
  • -

#3 wiredwizard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 07-May 05

Re: Help with cf array

Posted 07 May 2005 - 12:42 PM

error is being produced by this line

<cfset session.cart = arrayAppend( session.cart, structNew() )>
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Help with cf array

Posted 07 May 2005 - 12:51 PM

Try taking the StructNew() out of the array append and do something like this:

<cfset thisitem = structnew()>

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



I'm wondering if structnew is creating some sort of data type that isn't liking being called inside the array append function. I know you can store structures in an array but normally I setup my structure first, for example, you are appending your structure before populating it, you may try creating your structure, populating it's elements, then appending the structure to the array.
Was This Post Helpful? 0
  • +
  • -

#5 wiredwizard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 07-May 05

Re: Help with cf array

Posted 07 May 2005 - 01:07 PM

still getting same error :wacko:

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


The error occurred in C:\Inetpub\wwwroot\udemandsports\new\cart.cfm: line 16

14 : <cfparam name="thisItem" default="structnew()">
15 : <!--- append new element to array --->
16 : <cfset session.cart = arrayAppend( session.cart, thisitem )>
17 :
18 : <!--- set array position --->
Was This Post Helpful? 0
  • +
  • -

#9 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Help with cf array

Posted 07 May 2005 - 01:15 PM

can you try the same code but instead of appending it to session.cart append it to just a plain ole' cart variable, I have not read any documentation indicating you can't use structures/arrays in sessions, but it's possible I guess.

If that doesn't work, lets start working backwards.

See if this works:
<cfparam name="session.cart" default="">

<cfset session.cart = ArrayNew(1)>

<cfset thisitem = StructNew()>

<cfset thisitem.test = "Test">
<cfset thisitem.test2 = "Test2">

<cfoutput>#ArrayAppend(session.cart, thisitem)#</cfoutput>

<cfdump var="#session.cart#"> 



I put cfoutput tags around the arrayappend because if it works it will return the word YES, I also tried to break the code down a little to eliminate any problems higher up. I don't have CF on this box otherwise I would test myself, sorry :(
Was This Post Helpful? 1

#10 wiredwizard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 07-May 05

Re: Help with cf array

Posted 07 May 2005 - 03:59 PM

re: start working backwards

works was able to add form varriables after that thank you for your help skyhawk

I posted a couple of new coldfusion snnipets let me know what you think

:D
Was This Post Helpful? 0
  • +
  • -

#11 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Help with cf array

Posted 07 May 2005 - 04:02 PM

Sweet! Glad I could help.

Snippets are awesome, I'ma add the dumpall at work on Monday. Very handy!

Welcome to dream.in.code, thanks for the snippets, hope you find the site helpful!
Was This Post Helpful? 0
  • +
  • -

#12 shaunw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-April 07

Re: Help with cf array

Posted 23 April 2007 - 02:46 PM

View Postwiredwizard, on 7 May, 2005 - 03:59 PM, said:

re: start working backwards

works was able to add form varriables after that thank you for your help skyhawk

I posted a couple of new coldfusion snnipets let me know what you think

:D


I know it is sometime later..

But I was also having this issue and got the form values to add to the struct/array with your help above.

However I am unable to add more then one array element to the session.cart (containing the struct and form values).

Can you help me with this?

Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1