[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 763
  • Last Modified:

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>
0
Albee_J
Asked:
Albee_J
  • 7
  • 7
1 Solution
 
_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
 
Albee_JAuthor Commented:


This solution generates the following error:

Element ROWNUMBERS is undefined in VARIABLES.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
_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:
>> 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:
>> 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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now