ColdFusion Convert Query to an Array for Shuffle

Currently we have an array that spits out in a footer to shuffle our site sponsors

<cfset arrSponsors = ListToArray(
'<a href="http://www.fmanet.org/memberdirectory/member-details.cfm?id=102951">Clark Metal Products Co.</a>,
<a href="http://www.fmanet.org/memberdirectory/member-details.cfm?id=139608">General Sheet Metal Works Inc.</a>
')/>
<cfset CreateObject("java","java.util.Collections").Shuffle(arrSponsors)/>

<cfoutput>
    <cfloop array="#arrSponsors#" index="i">
        <dd>#i#</dd>
    </cfloop>
</cfoutput>

If I want to replace the hard-coded list with a query  how do I get that to shuffle?

  <cfloop query = "getSupporters">
 <cfoutput>
<dd><a href="#getSupporters.SiteSupportersURL#">#getSupporters.SiteSupportersName#</a></dd>
</cfoutput>
</cfloop>
Albee_JAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
>> For now I just threw this in

I totally forgot you were using MS SQL. Yeah, use that. It's the simplest for small data sets.
0
 
_agx_Commented:
This function from cflib should work
http://www.cflib.org/udf/QueryRandomRows

0
 
_agx_Commented:
.. or you could generate an array of row numbers and shuffle that. Then loop through the array and output the columns using array notation.

...
<cfloop from="1" to="#arrayLen(variables.rowNumbers)#" index="x">
 <cfset row = variables.rowNumbers[x]>
 <cfoutput>
      <dd><a href="#getSupporters.SiteSupportersURL[row]#">#getSupporters.SiteSupportersName[row]#</a></dd>
      </cfoutput>      
</cfloop>

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Albee_JAuthor Commented:


This solution generates the following error:

Element ROWNUMBERS is undefined in VARIABLES.
0
 
_agx_Commented:
Yeah, like I said you have to generate an array of row numbers and shuffle() it first ;-) It's just a simple cfloop from 1 to the query.recordCount and appending the counter number to the array.
0
 
Albee_JAuthor Commented:
ah ok will do
0
 
Albee_JAuthor Commented:
ok, I am completely confused as how to "generate an array of row numbers and shuffle() "

:/
0
 
Albee_JAuthor Commented:
For now I just threw this in

<cfquery name="getSupporters" datasource="#APPLICATION.DSN#">
            SELECT SupporterID, Name, URL
            FROM tFMA_SiteSupporters
    ORDER BY newid()
    </cfquery>
0
 
Albee_JAuthor Commented:
Is this the syntax?

<cfloop list="#getSupporters#" from="1" to="#getSupporters.recordCount#">

</cfloop>
0
 
_agx_Commented:
>> Is this the syntax?

No, it's just your basic from/to loop :)

     <cfloop from="1" to="#getSupporters.recordCount#" index="counter">  ... etc...

But don't bother with a loop The query solution you posted is better :)
0
 
Albee_JAuthor Commented:
Awesome! yay! :)
0
 
_agx_Commented:
Thanks for the points, but I would have accepted your answer because it was "the best solution" here :)
0
 
Albee_JAuthor Commented:
ok NOW you are just going to make me blush *laugh* :)
0
 
_agx_Commented:
LOL.  No need to be modest.  It's just the truth :) (Honestly, I'm feeling a little silly I forgot about that approach. But hey, you've got to be able to admit when you're wrong ;-)

Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.