开发者

Only show if item is not most recent?

So we have a news/announcement function that pulls info from a database and displays it

<cfquery name="announcement" datasource="#application.datasource#" dbtype="odbc">
SELECT top 2 * FROM NewsEvents
WHERE type = 2 AND active = 1 AND publish_datetime <= #now()# AND show_on_home = 1 开发者_如何学编程AND item_datetime >= #createOdbcDate(now())#
ORDER BY item_datetime ASC
</cfquery>

What we're trying to do is display a piece of HTML within these announcement blocks, conditional on one of the two blocks NOT being the most recent of the two blocks.

The HTML is wrapped in this code:

<cfif announcement.recordCount gt 0>
<cfloop query="announcement">
<cfoutput>
html
</cfoutput>
</cfloop>
<cfelse>
<cfoutput><p>There are currently no announcements.</p></cfoutput>
</cfif>

I want one component of the HTML to only be displayed for the BOTTOM announcement, the one that is not the closest in item_datetime. Any thoughts on how to accomplish this?


You're ordering by date, so just do something like this:

<cfif CurrentRow EQ 1>
    We're showing the first record, so show some stuff.
</cfif>

CurrentRow is always available in a loop to show you which record of the query you're currently on. So given your current query (and some made up data straight outta my head, you'll end up with this output:

Record 1: August 29, 2011
We're showing the first record, so show some stuff

Record 2: August 30, 2011

You're sorting by the date ASC, so the oldest record is first, while the newer record is last. I would recommend that you change your sorting to DESC, or you're never going to get newer records once you've added a third announcement after the first two. You could then resort them on the CF side to be in the display order you're after.


You can add a modifier to your CFQUERY statement like so. I would also modify your CFIF recordCount statement to look for more than 1 record at this point.

<cfif announcement.recordCount gt 1>
   <cfoutput query="announcement" startRow="2">
      <!--- Throw my output here --->
   </cfoutput>
<cfelse>
   <p>There are currently no announcements.</p>
</cfif>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜