Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating array of structs troubling

I am trying to create an array of structures, in my Application.cfm file, which can then be appended to in further pages. I am following the EasyCFM tutorial #173 by Charlie. I am using it this way:

<cfset session.box_status = arrayNew(1) />
<cfset session.box_status[1] = structNew() />
<cfset session.box_status[1].partner_id = '0' />
<cfset session.box_status[1].partner_username = '' />
<cfset session.box_status[1].status = '0' />

In my page, I am appending to the structure like so:

<cfloop from="1" to="#arrayLen(session.box_status)#" index="i">
  <cfset session.box_status[i].partner_id = ArrayAppend(i,FORM.partner_id) />
  <cfset session.box_status[i].partner_username = ArrayAppend(i,FORM.partner_username) />
  <cfset session.box_status[i].status = ArrayAppend(i,FORM.box_status) />
</cfloop>

But am getting an error:

    The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request
Object of type class java.lang.Double cannot be used as an array 
like image 508
voyeger Avatar asked Feb 18 '26 03:02

voyeger


2 Answers

In addition to Scott's comments, you need to clarify what you are actually trying to achieve. The question asks about appending a new item, yet it looks as if parts of your code attempt to overwrite the existing structure values in position session.box_status[1].

If you really want to append a new structure to the array, there is no reason to loop. Simply create an empty structure:

<cfset newItem = structNew() /> 

... populate it with some values:

<cfset newItem.partner_id = FORM.partner_id>
... etcetera

Then append the new structure to the array. Notice, the code below does not care about the result of ArrayAppend. That is because the function modifies the array in place, and only returns true/false depending on whether the action was successful.

<cfset ArrayAppend(session.box_status, newItem)>

Update:

That said, the tutorial you are using was obviously written for an older version of CF. As @cfqueryparam pointed out, later versions support a shorthand for creating arrays and structures. Instead of using structNew(), you could simply do this:

 <cfset newItem = { partner_id = FORM.partner_id, ... etectera }>
like image 69
Leigh Avatar answered Feb 21 '26 15:02

Leigh


The first argument in arrayAppend() needs to be the array to which you are appending something, in your example, you are using i - which is the counter of your loop - which is a number, not an array.

like image 28
Scott Stroz Avatar answered Feb 21 '26 15:02

Scott Stroz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!