Solved

Coldfusion - cfdirectory sort order question

Posted on 2013-05-16
4
545 Views
Last Modified: 2013-05-17
I have folder with images named like A199250-1.jpg,  A199250-2.jpg, A199250-11.jpg, etc...Each image name has a corresponding property listing id like: "A199250"

The problem is trying to sort these in the correct order.
Here is how they currently sort:
A199250-1.jpg
A199250-11.jpg
A199250-2.jpg



I need them to sort like:
A199250-1.jpg
A199250-2.jpg
A199250-11.jpg

<cfdirectory action="list" directory="#ExpandPath('/ftp/rets/photos/')#" filter="#variables.IDnumber#*" name="files" sort = "directory ASC">

Can someone have a look at my code and tell me how to accomplish this? Thanks!
0
Comment
Question by:Bang-O-Matic
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 39172136
There's no slick way to do this.  If QoQ's supported basic string functions like mid() etc.., you could do it in a single query. But QoQ's don't support those functions. So that's not an option.

What you could do is add 2 columns to the cfdirectory query object:

<!--- add new columns for sorting --->
<!--- for older versions, use ArrayNew(1) instead of "[]" --->
<cfset queryAddColumn(qList, "fileID", []) />
<cfset queryAddColumn(qList, "fileNumber", []) />

Then use a loop to split out the "id" and file "number"

<cfset delims = "-.">
<cfloop query="qList">
      <cfif listLen(qList.name, delims) eq 3>
            <!--- extract file id and counter *number* --->
            <cfset qList.fileID[currentRow] = getToken(qList.name, 1, delims)>
            <cfset qList.fileNumber[currentRow] = val(getToken(qList.name, 2, delims))>
      </cfif>
</cfloop>

Finally resort the results by the "id" then "number"

<!--- resort by id, then counter number --->
<cfquery name="qList" dbtype="query">
      SELECT *
      FROM   qList
      ORDER BY fileID, fileNumber, name
</cfquery>

<cfdump var="#qList#">

It's lame, but it works :)
0
 

Author Comment

by:Bang-O-Matic
ID: 39172636
Hi _agx_,

I did some research myself before posting so I knew it would not be simple to solve. :(
How would you work that code into my existing code? (This code runs on a property detail page and loads images into a slider based on the ListID.) Thanks for the help!

<cfset variables.IDnumber = #URL.ListID#>

<cfdirectory action="list" directory="#ExpandPath('/ftp/rets/photos/')#" filter="#variables.IDnumber#*" name="files" sort = "directory ASC">
       <cfif files.recordCount eq 0>
             
   <img src="../images/no-photo.png"  alt="no-photo" />

                  <cfelse>

       <cfloop query="files">
     
     <a class="rsImg" data-rsh="500" data-rsBigImg="/ftp/rets/photos/#files.name#" href="/ftp/rets/photos/#files.name#">
     <img width="96" height="72" class="rsTmb" src="/ftp/rets/photos/#files.name#" /></a>
     
    </cfloop>
    </cfif>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39172908
I'd probably throw the whole thing into a function that does the filtering and returns a query. Then call the function instead of <cfdirectory>. Everything else would be the same

Usage: (Call this instead of cfdirectory)

<cfset files = getListingsByID( ExpandPath('/ftp/rets/photos/'), URL.ListID )>

Function (Double check that everything's VAR scoped)
<cffunction name="getListingsByID" returntype="query">
	<cfargument name="directoryPath" type="string">
	<cfargument name="idNumber" type="string">

	<cfset var delims = delims = "-." />
	<cfset var qList = delims = "-." />
	
	<cfdirectory action="list" 
			directory="#arguments.directoryPath#" 
			filter="#arguments.idNumber#*" 
			name="qList" 
			sort="directory ASC">	

	<cfset queryAddColumn(qList, "fileID", []) />
	<cfset queryAddColumn(qList, "fileNumber", []) />

	<cfloop query="qList">
		<cfif listLen(qList.name, delims) eq 3>
			<!--- extract file id and counter *number* --->
			<cfset qList.fileID[currentRow] = getToken(qList.name, 1, delims)>
			<cfset qList.fileNumber[currentRow] = val(getToken(qList.name, 2, delims))>
		</cfif>
	</cfloop>

	<!--- resort by id, then counter number --->
	<cfquery name="qList" dbtype="query">
		SELECT *
		FROM   qList
		ORDER BY fileID, fileNumber, name
	</cfquery>

	<cfreturn qList />
</cffunction>

Open in new window

0
 

Author Closing Comment

by:Bang-O-Matic
ID: 39174820
Excellent solution that worked perfectly! Thank you _agx_ !
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now