jeremiahx|com J.J. Merrick on Facebook jeremiahx on Twitter

[CF101] Finding the first row in a recordset

A question was posed on the cf-newbie list asking about how to format the first record in a recordset via CSS.

Hi All -

I am producing a front page of a site where I need to have the latest news item and blog entry formatted (via CSS) differently than the later entries. The first record of the news items and blog entry will be formatted with background color and larger font where the later news items and entries will be plain. I believe it has something to do with counting the records in the query, so I’ve tried something like:

<cfquery name=”getNewsItems” datasource=”myDSN”>
SELECT TOP 5 headline, releaseDate, id
FROM NewsItems
Order by releaseDate
</cfquery>

<h2>News</h2>
<cfif getNewsItems.RecordCount is “1″>
<p class=”news_events_feature”>
<cfoutput query=”getNewsItems”>
<img src=”img/hm/news_events/#id#

.jpg” width=”85″ height=”96″ />
#headline#<br />
<a href=”##” class=”right”></cfoutput>Read More >></a>
</p>

<cfelseif getNewsItems.RecordCount is not “1″>
<ol>
<li><cfoutput query=”getNewsItems”><a href=”##”>#headline#<br /><br /></a></cfoutput></li>
</ol>
<a href=”##” class=”more”>More News & Events>></a>
</cfif>

Can anyone tell me why my thinking is not correct?

Thanks,
Adam

A RecordCount gives you the number of records in the query. What you need to use is

queryname.currentRow

and since it is a number just do

<cfif getNewsItems.currentRow EQ 1>

<cfelse>

</cfif>
Easy as pie, your queryname.currentrow will tell you what you row number you are on. Another cool thing is to use MOD to decide if it is every other row to do a grey/white pattern… but that is another blog post!

Leave a Comment