I will preface this by stating you will probably not want to do this. However, I figured I would blog about it anyways as the outcome was very strange.

In a system I was working on I was attempting to reuse some code to expedite some new development. What I did was create a wrapper for an existing cfm. I used cfinclude to bring in the code we were going to reuse and then looped over it. The code that was being looped generated images for output to a browser. So instead of an image tag pointing to a .jpg for instance, it pointed to our image.cfm. I was looping this image code to generate large amounts of images in one call.

Here is the code that was doing the loop and returning an array of what was done. The image.cfm file was expecting url vars so we just set them prior to the include. We then built an array of the image path generated and the url var structure. This way the code could then loop through was was generated later.

view plain print about
1<CFSET ResArray = arrayNew(2)>
2
3<CFLOOP QUERY="getItems">
4     <CFSET url.a = item_id>
5    <CFSET url.height = 300>
6    <CFSET url.width = 200>
7    <CFINCLUDE TEMPLATE="image.cfm">
8    <CFSET ResArray[i][1] = 'generated image path'>
9    <CFSET ResArray[i][2] = url>
10</CFLOOP>

The strange part in all this was what came out at the end in the array. Every second dimension of the array was identical. It was always the information from the last row in the query. So this leads me to believe that even though I am setting the second dimension of the array what the url struct is it does not matter. It appears to just make a reference to the url struct.

I then went on to do some further checking. I tried changing the line that sets dimension 2 to the url struct to this:

view plain print about
1<CFSET ResArray[i][2] = structCopy(url)>

This had no effect. The output was still the same. Then to throw another wrench into the works I added this gem to the code:

view plain print about
1<CFIF currentRow EQ 5>
2    <CFSET url.extra = 5>
3</CFIF>

I knew there were 10 rows in the return so I picked the middle row. What happened was strange. Every array element generated from 5 - 10 contained the "extra" item. Where rows 1 - 4 did not. However, all the items still had all the rest of their values from the last array item.

Then I tried to use structDelete then structnew at the start of each loop. This had not effect except for one strange thing. The "extra" item vanished from all output, even row 5.

There you have it. I am not sure where to go from here except back to the drawing board and rewrite some code into a CFC. I probably should have converted it to a CFC in the first place.

Till next time,

--Dave