Solved

ColdFusion Convert Query to an Array for Shuffle

Posted on 2010-11-10
14
739 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Database set up 5 87
How to resolve "Resouce Busy" error generated by Coldfunsion function 20 83
Coldfusion : Unix Epoch Time 4 54
Application.cfm not found issue 2 64
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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