abrabr
abrabr

Reputation: 217

last and first content in loop or output (coldfusion)

I wonder if there is possible to display specific content taken from the Loop or Output in ColdFusion, for example i have an output:

<cfoutput query="get_service_plus"><b>#SUBJECT#</b><br/>#plus_content#<br/></cfoutput>

and its query, just in case:

<cfquery name="GET_SERVICE_PLUS" datasource="#DSN3#">
    SELECT 
        *
    FROM 
        SERVICE_PLUS
    WHERE   
        SERVICE_ID = #attributes.action_id#
      <cfif isDefined("GET_SERVICE_PLUS.SERVICE_PLUS_ID")>
        AND SERVICE_PLUS_ID = #GET_SERVICE_PLUS.SERVICE_PLUS_ID#
      </cfif>
      ORDER BY PLUS_DATE DESC,RECORD_DATE DESC
</cfquery>

i know that i most probably should use the loops to get the specific content from db but couldn't understand how to achieve it... thx for help!

Upvotes: 8

Views: 7792

Answers (2)

Ciaran Archer
Ciaran Archer

Reputation: 12466

I think this should do it from a CF perspective:

<cfoutput query="get_service_plus">

  <cfif get_service_plus.currentRow eq 1 or get_service_plus.currentRow eq get_service_plus.recordCount>
    <b>#SUBJECT#</b><br/>#plus_content#<br/>
  </cfif>

</cfoutput>

You could also do this using a query of queries once you have the initial recordset back.

I would suggest you try and grab the first and last row at the database layer like you suggest as that is much more efficient, especially for large record sets.

Hope that helps.

Upvotes: 0

Stephen Moretti
Stephen Moretti

Reputation: 2438

There are a couple of things you can do.

If you are looping the complete query you can check your current row number by using the variable "qet_service_plus.currentrow", so

<cfif qet_service_plus.currentrow eq 1>
   <!--- do first row display stuff --->
</cfif>

With every query also comes the number of records returned in the query. You can find this in "recordcount", so

<cfif get_service_plus.currentrow eq get_service_plus.recordcount>
   <!--- do last row display stuff --->
</cfif>

If you want to get to a specific record in a query without going through the complete query you can treat a cfquery as an associative array. eg.

<cfoutput>
<!--- service id in first record --->
#get_service_plus['service_id'][1]# 
<!--- service id in last record --->
#get_service_plus['service_id'][get_service_plus.recordcount]# 
</cfoutput>

Upvotes: 15

Related Questions