Solved

ColdFusion Convert Query to an Array for Shuffle

Posted on 2010-11-10
14
747 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
[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
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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
 

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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