Solved

Coldfusion - cfdirectory sort order question

Posted on 2013-05-16
4
531 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_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent solution that worked perfectly! Thank you _agx_ !
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

11 Experts available now in Live!

Get 1:1 Help Now