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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Index a Variable During a CFloop? 1

Status
Not open for further replies.

3dColor

Programmer
Jan 10, 2006
240
US
Instead of repeating the same code over and over 15 times:

Code:
<cfif GetNewImages.photo1 NEQ "">
	- Lots of code to process the image on to my server -
</cfif>

<cfif GetNewImages.photo3 NEQ "">
	- Lots of code to process the image on to my server -
</cfif>

<cfif GetNewImages.photo4 NEQ "">
	- Lots of code to process the image on to my server -
</cfif>

    ...repeat until 15

I would rather do something like this:

Code:
<cfloop
    index="image"
    from="1"
    to="15"
    step="1">

    <cfif GetNewImages.photoX NEQ "">
		- Lots of code to process the image on to my server -
    </cfif>
    
</cfloop>

But how do I loop through the variable GetNewImages.photoX 15 times?

GetNewImages.photo1
GetNewImages.photo2
GetNewImages.photo3
etc.
 
or you can avoid evaluate which has a lot of overhead and use structure syntax.

ex.
Code:
<cfset x = evaluate("form.fieldName" & i)>
would be
Code:
<cfset x = form["fieldName" & i]>
or
Code:
<cfset x = form["fieldName#i#"]>

I prefer the first alternative syntactically; however, the second option is a bit more efficient according different concatenation tests I've run.

In the case of your query you would want to use
Code:
<cfif variables.getNewImages["photo#image#"][getNewImages.currentRow] neq "">
if getNewImages isn't a query you would simply use
Code:
<cfif variables.getNewImages["photo#image#"] neq "">


Vegans are friends, not food...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top