• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Random Display of Query Results (no repeats)

New Here ,
May 12, 2006 May 12, 2006

Copy link to clipboard

Copied

First of all, I am a ColdFusion novice, so please forgive me if the answer to this is either blantantly obvious or totally impossible. Here is my problem.

I am querying a database with several hundred records. The query will return on average between 10 - 15 records. I then want to display 2 of these records on a random basis. I do have this much working. The only problem is that every so often, the 2 records that are displayed are the same record. I need to avoid this outcome. The 2 records should always be different.

Here is a simplified version of my code:

<CFQUERY name="queryname" datasource="datasourcename">
SELECT * FROM Residential
WHERE ListingOfficeName = 'Widner Realty'
</CFQUERY>

<cfset randRecordsList = ''>
<cfloop from="1" to="2" index="i">
<cfset randVal = randRange(1,queryname.recordCount)>
<cfset randRecordsList = listAppend(randRecordsList , randVal)>
</cfloop>

<cfloop list="#randRecordsList#" index="i">
<cfoutput>
#queryName.FieldName1 #
#queryName.FieldName2
#
#queryName.FieldName3 #<br>
</cfoutput>
</cfloop>

Any help would be appreciated. Thanks in advance.
TOPICS
Advanced techniques

Views

262

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 12, 2006 May 12, 2006
Here is the general idea. You can look after the specifics.

// generate list of numbers.
maxnum = 15;
numlist = "";
for (i = 1; i lte maxnum; i = i + 1)
numlist = listappend(numlist, i);

// randomnums will have the two random numbers
RandomNums = "";
x = true;
while (x is true) {
ThisNum = RandRange(1, maxNum);
if (ListFind(RandomNums, ThisNum) is false)
RandomNums = ListAppend(RandomNums, ThisNum);

if (ListLen(RandomNums) is 2)
x = false;
}

Votes

Translate

Translate
LEGEND ,
May 12, 2006 May 12, 2006

Copy link to clipboard

Copied

Here is the general idea. You can look after the specifics.

// generate list of numbers.
maxnum = 15;
numlist = "";
for (i = 1; i lte maxnum; i = i + 1)
numlist = listappend(numlist, i);

// randomnums will have the two random numbers
RandomNums = "";
x = true;
while (x is true) {
ThisNum = RandRange(1, maxNum);
if (ListFind(RandomNums, ThisNum) is false)
RandomNums = ListAppend(RandomNums, ThisNum);

if (ListLen(RandomNums) is 2)
x = false;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 12, 2006 May 12, 2006

Copy link to clipboard

Copied

LATEST
Thanks for the help. I got it working using your suggestions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 12, 2006 May 12, 2006

Copy link to clipboard

Copied

Oh, by the way, make sure you have at least two records before trying that code or you will end up in an infinite loop.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation