ColdFusion cfloop skip row or change loop endRow

1.5k Views Asked by At

I am trying to display 2 images, and skip one if it matches the following criteria (<cfif myDir.name eq property_mainimage> which works fine, and then continue onto the next image and display the next image until I have a total of 2 images. But when the criteria image is say in row 5, the loop displays 3 images, when the criteria image is say row 1, it displays 2 images.

There doesn't seem to be a skip row in cfloop?

I have tried <cfif counter gt 3 > <cfabort> which works but it displays the 3 images still of the criteria image is in a row greater than 3. Below is what i'm working with...

<cfdirectory directory="C:\Domains\domain.com\wwwroot\uploads\images\#property_ID#\" filter="*.jpg" name="myDir" type="file" sort="datelastmodified">
<ul>
  <cfset counter = 0>
  <cfset endrowvalue = 3 >
  <cfloop query="myDir" endRow="#endrowvalue#">
    <cfset counter = counter +1 >
    <li>
    <cfif right(myDir.name,4) is ".jpg">
      <cfif fileexists("C:\Domains\ domain.com\wwwroot\uploads\ images\#property_ID#\#left(mydir.name,len(myDir.name)-4)#.jpg")>
        <cfif myDir.name eq property_mainimage>
          DO NOT SHOW THIS IMAGE
        <cfelse>
          <cfset endrowvalue = 1 >
          <cfset myDir.endRow = 1 >
          test #endrowvalue#
          test #counter# <img src="#request.root#uploads/images/#property_ID#/#myDir.name#" border="0" width="230px" style="margin-bottom:15px;"  />
        </cfif>
      </cfif>
    </cfif> 
    </li>
  </cfloop>
</ul>

any help would be most appreciated.

2

There are 2 best solutions below

0
Matt Busche On

I would use a counter to count when you show the image. Once you get to 2 images displayed break out of the loop.

<cfdirectory directory="C:\Domains\domain.com\wwwroot\uploads\images\#property_ID#\" filter="*.jpg" name="myDir" type="file" sort="datelastmodified">
<ul>
  <cfset counter = 1>
  <cfloop query="myDir">
    <li>
      <cfif right(myDir.name,4) is ".jpg">
        <cfif fileexists("C:\Domains\ domain.com\wwwroot\uploads\ images\#property_ID#\#left(mydir.name,len(myDir.name)-4)#.jpg")>
          <cfif myDir.name eq property_mainimage>
            DO NOT SHOW THIS IMAGE
          <cfelse>
            <!--- image displayed increment coutner --->
            <cfset counter++>
            <img src="#request.root#uploads/images/#property_ID#/#myDir.name#" border="0" width="230px" style="margin-bottom:15px;"  />
          </cfif>
        </cfif>
      </cfif> 
      <cfif counter EQ 2>
        <cfbreak>
      </cfif>
    </li> 
  </cfloop>
</ul>
0
Dan Bracuk On

This is a formatted comment. Your code has this:

<cfset endrowvalue = 3 >
<cfloop query="myDir" endRow="#endrowvalue#">
    <cfif myDir.name eq property_mainimage>
      DO NOT SHOW THIS IMAGE
    <cfelse>
      <cfset endrowvalue = 1 >

You would have to test it to be sure, but I suspect that the endRow attribute of your cfloop tag will continue to be 3, even if you change the value of the endrowvalue variable to 1.

This might not be the cause of your current problem, but, it my suspicians are correct, you will either have a problem elsewhere or you are running a pointless command.