Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable inside a variable is its own variable? 1

Status
Not open for further replies.

Chloeee

Programmer
Jan 16, 2001
27
0
0
US
I've been losing sleep over this one and would appreciate anyone who can find the solution. I did a work-around that involves about 600 lines of code (it's insane, I know!) because I was out of time and had to deliver the product, but now I really need to make it proper. So...

It's the admin for a photo gallery. The user uploads any amount of photos into a temp directory via activex control. CFDIRECTORY reads the contents of the temp directory and outputs for each photo (using a loop, of course) a thumbnail and a text input box for the user to input a caption. To differentiate each caption, I've dropped in a counter that names the caption fields PhotoCaption1, PhotoCaption2, etc... Now, on the receiving end of this form I need to loop over each PhotoCaption and insert it into the database (as well as some other tasks). Now what I can't figure out is how to have one variable inside another one and have it be a variable itself. Perhaps looking at the code below will demonstrate what I mean. Theoretically, this code would work. But of course, CF melts down. *sigh*

[red]
Code:
<cfdirectory action=&quot;LIST&quot; directory=&quot;C:\[URL unfurl="true"]www\mydomain.com\photos\photofiles\temp&quot;[/URL] name=&quot;temp_photos&quot; filter=&quot;*.jpg&quot;>
<cfoutput query=&quot;temp_photos&quot;>
  <cfset theNumber = theNumber + 1>
  <cfquery name=&quot;insert_photo&quot; datasource=&quot;mydsn&quot;>
    INSERT INTO Photo (EventID, PhotoCaption)
    VALUES (#EventID#, '#Form.PhotoCaption.#theNumber##')
  </cfquery>
</cfoutput>
[/red]
 
Try this.

Assume theNumber is initialised.
Code:
<cfdirectory action=&quot;LIST&quot; directory=&quot;C:\[URL unfurl="true"]www\mydomain.com\photos\photofiles\temp&quot;[/URL] name=&quot;temp_photos&quot; filter=&quot;*.jpg&quot;>
<cfoutput query=&quot;temp_photos&quot;>
  <cfset theNumber = theNumber + 1>
  <cfset field = &quot;FORM.PhotoCaption&quot; & #theNumber#>
  <cfquery name=&quot;insert_photo&quot; datasource=&quot;mydsn&quot;>
    INSERT INTO Photo (EventID, PhotoCaption)
    VALUES (#EventID#, '#Evaluate(field)#')
  </cfquery>
</cfoutput>

I have to admit that the use of the Evaluate() function takes some getting used to but once you master it, it will prove to be indispensible.

Duncan
 
The Evaluate() function can be useful, but I try to avoid it anywhere possible because it's slower than other methods. One or 2 Evaluate() functions in a page wouldn't make a significant difference, but more than that would.

Another way to approach this is to look at the FORM scope as an Array or Structure. So instead of accessing the FORM variables as #FORM.PhotoCaption1#, #FORM.PhotoCaption2#, you can also access them as #FORM
Code:
[
&quot;PhotoCaption1&quot;
Code:
]
#
, #FORM
Code:
[
&quot;PhotoCaption2&quot;
Code:
]
#
.

So with this in mind, here's a little faster way of doing it without using the Evaluate() function.

<cfdirectory action=&quot;LIST&quot;
directory=&quot;C:\www\mydomain.com\photos\photofiles\temp&quot;
name=&quot;temp_photos&quot; filter=&quot;*.jpg&quot;>


<cfoutput query=&quot;temp_photos&quot;>
<cfset theNumber = theNumber + 1>
<cfquery name=&quot;insert_photo&quot; datasource=&quot;mydsn&quot;>
INSERT INTO Photo (EventID, PhotoCaption)
VALUES (#EventID#,
'#Form
Code:
[
&quot;PhotoCaption#theNumber#&quot;
Code:
]
#
'
)
</cfquery>
</cfoutput> - tleish
 
Fantastic. Never knew you could do that. I just changed a file which relies heavily on Evaluate() and it reduced the the time the page took by 50-60%.

Definitely a better solution than my Evaluate()
 
FYI, you can use this same technique with:

QUERIES: #qMyQuery
Code:
[
RowNum
Code:
]
Code:
[
&quot;ColName#variable#&quot;
Code:
]
#
FORM: #FORM
Code:
[
&quot;FieldName#variable#&quot;
Code:
]
#
REQUEST: #REQUEST
Code:
[
&quot;Name#variable#&quot;
Code:
]
#
SESSION: #SESSION
Code:
[
&quot;Name#variable#&quot;
Code:
]
#
CLIENT: #CLIENT
Code:
[
&quot;Name#variable#&quot;
Code:
]
#
SERVER: #SERVER
Code:
[
&quot;Name#variable#&quot;
Code:
]
#

...or on any Structures that you create yourself - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top