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

Having a problem creating a Dynamic CFIF

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
GB
Hi,

I have a number of fields called:

Field_0
Field_1
Field_2
Field_3
etc

the only problem is that the above fields are created dynamicaly, there could be 50 or there could be none. The problem is that when I want to check if they are defined and that they do NOT contain "" (nothing). How do I create a CFIF command based on the above.

The number of fields are know, but only through a variable (FieldNumbers).

I have tried putting a CFIF inside of a CFLOOP, but i just get errors saying I need a </CFIF>.

How is it done ????

Cheers,
Mark
 
Is there a way you could check the dimension of your table first, then do a condition based on that dimension ? just a thought.
 
Perhaps it would be easier to throw the results from your fields into an array, then work with the array values instead of the fields directly.
 
Try something like:

<cfset flds = &quot;&quot;>
<cfloop from=&quot;1&quot; to=&quot;#fieldnumber#&quot; index=&quot;x&quot;>
<cfset flds = listappend(flds,&quot;isdefined('field_#x#')&quot;,&quot;|&quot;)>
</cfloop>
<cfset flds = replace(flds,&quot;|&quot;, &quot; AND &quot;,&quot;ALL&quot;)>
<cfif #flds#> <!--- This may need to be <cfif evaluate(#flds#)> --->
...
<cfelse>
...
</cfif>
 
Try this quick example:
Code:
<cfset fieldnumbers = 0>
<cfset FORM.field_0 = &quot;hi there&quot;>

<cfloop from=&quot;0&quot; to=&quot;#FieldNumbers#&quot; index=&quot;ii&quot;>
<cfif Len(Trim(FORM[&quot;field_&quot; & ii]))>
   <!--- This form field is not blank --->
   <cfoutput>#FORM[&quot;field_&quot; & ii]#</cfoutput>
</cfif>
</cfloop>
The only caveat is that if FieldNumbers contains a &quot;1&quot;, the loop will think there are two fields, since you are starting your field names at 0 (i.e. FORM.field_0). You're better off starting them at 1.


-Tek
 
<cfloop index = &quot;fieldNum&quot; from = &quot;1&quot; to = &quot;#fieldnumbers #&quot;>
<cfif IsDefined(&quot;FORM.field_#fieldNum#&quot;) AND #trim(evaluate(&quot;FORM.field_&quot;&fieldNum))# NEQ &quot;&quot;>
do some stuff
</cfif>
</cfloop>

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
- Quote by Douglas Adams
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top