Solved

ColdFusion Convert Query to an Array for Shuffle

Posted on 2010-11-10
14
723 Views
Last Modified: 2012-06-27
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
Comment
Question by:Albee_J
  • 7
  • 7
14 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 34103844
This function from cflib should work
http://www.cflib.org/udf/QueryRandomRows

0
 
LVL 52

Expert Comment

by:_agx_
ID: 34104020
.. 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
 

Author Comment

by:Albee_J
ID: 34104381


This solution generates the following error:

Element ROWNUMBERS is undefined in VARIABLES.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34104462
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
 

Author Comment

by:Albee_J
ID: 34105024
ah ok will do
0
 

Author Comment

by:Albee_J
ID: 34106032
ok, I am completely confused as how to "generate an array of row numbers and shuffle() "

:/
0
 

Author Comment

by:Albee_J
ID: 34106118
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Albee_J
ID: 34106260
Is this the syntax?

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

</cfloop>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 34108141
>> 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
 
LVL 52

Expert Comment

by:_agx_
ID: 34108364
>> 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
 

Author Comment

by:Albee_J
ID: 34112361
Awesome! yay! :)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34112702
Thanks for the points, but I would have accepted your answer because it was "the best solution" here :)
0
 

Author Comment

by:Albee_J
ID: 34112728
ok NOW you are just going to make me blush *laugh* :)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34113684
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

16 Experts available now in Live!

Get 1:1 Help Now