Solved

Coldfusion - cfdirectory sort order question

Posted on 2013-05-16
4
558 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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