3 Replies Latest reply on May 12, 2006 12:36 PM by image_maker1

    Random Display of Query Results (no repeats)

    image_maker1
      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.