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

    Random Display of Query Results (no repeats)

      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'

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

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

      Any help would be appreciated. Thanks in advance.