Coldfusion: numbering database results

Discussion in 'Programming' started by lespaul00, Dec 18, 2007.

  1. #1
    I have a query that returns the top 25 records of one field of my database table.

    
    <cfquery name="top" datasource="mydatasource">
    SELECT TOP 25 RATING, CATEGORY
    FROM MYTABLE
    ORDER BY RATING
    </cfquery>
    
    Code (markup):
    I'm not sure if this is the exact coding I use, but whatever...

    I want to be able to have the results listed to have a list number before them (i.e. 1, 2, 3, 4, 5,...) so the user can see what ranking each CATEGORY has. So, I simply need to create a column that just numbers the results.

    Do I need to use the "count" function to determine the number of results, then do some sort of incremental numbering? I mean, I already know I will only have 25...
     
    lespaul00, Dec 18, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    when you output them just use #currentrow# where you want the numbers..

    i.e.
    <cfoutput query="top">
      <tr>
        <td>#currentrow#</td>
        <td>#category#</td>
      </tr>
    </cfoutput>
    Code (markup):
    by the way i am not advocating the use of tables in this situation.

    however.. if this is just going to be a standard list
    <ol>
      <cfoutput query="top">
        <li>#category#</li>
      </cfoutput>
    </ol>
    Code (markup):
    these are both assuming the ratings will always be in the set from 1 to 25
     
    Jamie18, Dec 19, 2007 IP
  3. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great, thank you. So #currentrow# is a standard CF function that will always work? Or, do I need to define it somehow, say, in my query?
     
    lespaul00, Dec 19, 2007 IP
  4. something1000

    something1000 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    query_name.currentRow


    The current row of the query being processed by cfoutput.

    http://livedocs.adobe.com/coldfusion/6.1/htmldocs/main_a10.htm

    ... because you are in this tag: <cfoutput query="top">
    you can simply use #currentRow#
     
    something1000, Dec 23, 2007 IP