aceslowman
aceslowman

Reputation: 631

Problems with setting up a CFIF

<cfoutput query="allOutcomes" maxrows="10">
  <div class="galleryOutcome">
    <cfset thisPhoto = uploads.listPhotobyOutcomeID(#outcomeID#)>
    <h3>#lastname#, #firstname#</h3>
    <cfloop query="thisPhoto" >
      <cfif isdefined(filename)>

        <div class="gallerythumb">
          <a href="javascript: thumbExpand('#fileName#')"><img src="documents/uploads/PHOTOS/#filename#" alt="#filename#" border="0"  width="200"/></a>
        </div>

      <cfelse>
        <p> NO PHOTOS </p>
      </cfif>
    </cfloop>

  </div><div class="clear"></div><br /><br />
  <div onClick="javascript: thumbHide()" id="thumbexpand" style="display:none; left:670px;; height:0px; position:fixed; top:100px;">                    
  </div>

</cfoutput>

I have been trying to make it so that #lastname# and #firstname# do not display if there are no photos associated to them. I tried doing a cfif that checks to see if the filename is defined, but it didn't seem to work. It returns an error saying:

"Parameter 1 of function IsDefined, which is now (filepath to image), must be a syntactically valid variable name. "

Any tips?

Thanks

Upvotes: 0

Views: 258

Answers (3)

Leigh
Leigh

Reputation: 28873

First, IsDefined expects the name of a variable. When you omit quotes, or use # signs, you are passing in the variable value instead. The correct syntax is:

   <cfif IsDefined("variableName")>

However, query columns always exist. So it will not yield the correct result anyway. Instead you should test if the FileExists. If needed, use expandPath to generate an absolute physical path

   <cfif FileExists( ExpandPath("/path/to/images/"& thisPhoto.fileName) )>
        it exists. do something ...
   <cfelse>
        no photo
   </cfif>

Edit: As Busches mentioned in the comments, generally structKeyExists is preferred over IsDefined because its results are more precise. Some may argue it also has better performance. But in most cases, any differences are negligible. Increased accuracy is the more compelling reason IMO.

<cfif structKeyExists( scopeOrStruct, "variableName")>

Upvotes: 16

Henry
Henry

Reputation: 32915

use <cfif len(filename)>

I guess filename is one of the columns? In a query object, null is represented with empty string, so len() would work.

Upvotes: 1

Yisroel
Yisroel

Reputation: 8174

isDefined takes the name of the variable as a string, not the variable itself. change

<cfif isdefined(filename)>

to

<cfif isdefined("filename")>

Upvotes: 1

Related Questions