Putting a loop variable within a variable

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
I'm working on a basic photo upload script and there are some chunks of code that repeat like a loop, but I've had to write them out an explicit amount of times because I can't put a variable within a variable.

Every instance has variable names that increment, such as "primary_1" to "primary_2".

Primary_1 is a variable. In order to loop through that, I would need to make a variable within a variable.

like #primary_#loop##

or worse

#photo_info_#loop#.profile_photo_public_url#

Here is a chunk of code that explains what I am talking about. If someone can show me how to reduce this 2 block chunk of code into 1 block with a loop clause, I would be forever grateful. I could reduce a 3,000 line script down to about 1,000 lines.

<CFIF isDefined("set_as_primary") AND (FORM.set_as_primary EQ "primary_1")>

<CFQUERY NAME="update_profile_thumbnail" datasource="members">

UPDATE tbl_profiles

SET profile_thumbnail_url = '#photo_info_1.profile_photo_public_url#',
profile_thumbnail_file = '#photo_info_1.profile_photo_file#'

WHERE (member_id = #MYID#)
;

</CFQUERY>

</CFIF>

<CFIF isDefined("set_as_primary") AND (FORM.set_as_primary EQ "primary_2")>

<CFQUERY NAME="update_member_profile_thumbnail" datasource="members">

UPDATE tbl_profiles

SET profile_thumbnail_url = '#photo_info_2.profile_photo_public_url#',
profile_thumbnail_file = '#photo_info_2.profile_photo_file#'

WHERE (member_id = #MYID#)
;

</CFQUERY>

</CFIF>
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
looking at that now. .thanks.. any idea on how i would write into my code? example? it would help me visualize its use.
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
We've been over this before...check out our last thread on the discussion of dynamic variable names in ColdFusion: http://forums.anandtech.com/me...256908&highlight_key=y

To be absolutely honest, it sounds to be like your data model is truly screwed up. I cannot think of any good reason why you would ever need to reference more than one image at a time in any scope of work. Perhaps you might consider using a function to perform some of these tasks, which would allow you to not really have to work so much with dynamic variable names like this. Alternatively, you could look at using an array of structs, rather than a different struct for each image. But, I would definitely rethink your data model.