Solved

Coldfusion - cfdirectory sort order question

Posted on 2013-05-16
4
622 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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