The code that I wrote exists inside of a function, which makes it  
modular and reusable.  By randomizing the query results as you display  
them, you would have to copy and paste that code everywhere you wanted  
to display query results in a random order rather than being able to  
call one function from several different places.  I create a new query  
object because my function is not concerned with the display of the  
results -- it leaves the presentation in the hands of the caller which  
keeps it generic and reusable.

Christian

On Sunday, December 22, 2002, at 02:32 PM, Joe Eugene wrote:

> I am not sure.. why you are creating a NEW query...
> This below.. should work..except..randRange(1,20).. can
> have duplicates..a list value check in the loop should do it.
>
> <cfscript>
> aIdx=arrayNew(1);
> for(i=1;i lte queryName.recordCount; i=i+1){
> aIdx[i]=randRange(1,queryName.recordCount);
> }
> </cfscript>
> <cfoutput>
> <cfloop index="k" from="1" to="#arrayLen(aIdx)#">
> #aIdx[k]# . #queryName.ColumnName[aIdx[k]]#<br>
> </cfloop>
> </cfoutput
>
> Joe
>
>> -----Original Message-----
>> From: Christian Cantrell [mailto:[EMAIL PROTECTED]]
>> Sent: Saturday, December 21, 2002 9:39 PM
>> To: CF-Talk
>> Subject: Re: Randomization of a cfquery
>>
>>
>> It's not as easy as it seems.  Here's a function that I believe will  
>> do
>> the trick.  I wrote it pretty quickly, so if you are going to use it,
>> test it thoroughly.  Just pass in a query object and it will return a
>> new query object with the rows randomized.  You can wrap it in a  
>> custom
>> tag if you like, but it makes a good function in my opinion.
>>
>> <cfscript>
>> function queryRandomize(toRandomize)
>> {
>>      var size = toRandomize.recordcount;
>>      var cols = toRandomize.columnList;
>>      var colArray = listToArray(cols);
>>      var newQuery = queryNew(cols);
>>      var i = 0;
>>      var j = 0;
>>      var randomRow = 0;
>>
>>      //
>>      // Create an array of integers that is the size of the record  
>> set.
>>      // We will draw on this array randomly and resize it dynamically.
>>      //
>>      var numberBank = arrayNew(1);
>>      for (i = 1; i neq size + 1; i = i + 1)
>>      {
>>          numberBank[i] = i;
>>      }
>>
>>      queryAddRow(newQuery, size);
>>      for (i = 1; i neq (size + 1); i = i + 1)
>>      {
>>          randomRow = randRange(1, arrayLen(numberBank));
>>          for (j = 1; j neq (arrayLen(colArray) + 1); j = j + 1)
>>          {
>>              querySetCell(newQuery, colArray[j],
>> toRandomize[colArray[j]][i], numberBank[randomRow]);
>>          }
>>          arrayDeleteAt(numberBank, randomRow);
>>      }
>>      return newQuery;
>> }
>> </cfscript>
>>
>> On Saturday, December 21, 2002, at 04:25 PM, David Jones wrote:
>>
>>> Does anyone have a custom tag that will take a regular query and
>>> randomize
>>> the results? I don't have to have a custom tag a database solution
>>> would do
>>> as well. Anything would help at this point. This seems like a simply
>>> thing
>>> to do.  I am using Oracle for my backend so if anyone knows of a way
>>> to do
>>> it there that would be great too.
>>>
>>> Thanks in advance,
>>>
>>> Dave
>>>
>>>
>>>
>>>
>>>
>>
> 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
This list and all House of Fusion resources hosted by CFHosting.com. The place for 
dependable ColdFusion Hosting.

Reply via email to