Avatar of Albee_J
Albee_J
 asked on

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>
ColdFusion Language

Avatar of undefined
Last Comment
_agx_

8/22/2022 - Mon
_agx_

This function from cflib should work
http://www.cflib.org/udf/QueryRandomRows

_agx_

.. 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>

Albee_J

ASKER


This solution generates the following error:

Element ROWNUMBERS is undefined in VARIABLES.
Your help has saved me hundreds of hours of internet surfing.
fblack61
_agx_

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.
Albee_J

ASKER
ah ok will do
Albee_J

ASKER
ok, I am completely confused as how to "generate an array of row numbers and shuffle() "

:/
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Albee_J

ASKER
For now I just threw this in

<cfquery name="getSupporters" datasource="#APPLICATION.DSN#">
            SELECT SupporterID, Name, URL
            FROM tFMA_SiteSupporters
    ORDER BY newid()
    </cfquery>
Albee_J

ASKER
Is this the syntax?

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

</cfloop>
ASKER CERTIFIED SOLUTION
_agx_

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
_agx_

>> 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 :)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Albee_J

ASKER
Awesome! yay! :)
_agx_

Thanks for the points, but I would have accepted your answer because it was "the best solution" here :)
Albee_J

ASKER
ok NOW you are just going to make me blush *laugh* :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
_agx_

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