Published by Mijingo

tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

Using {if count == total_results}

  • Posted by Christy Collins
  • May 31, 2009

In the EE docs, the description for the count conditional doesn’t clearly illustrate how useful this conditional can be.  You have available

{if count == '1'}...{/if} 
and 
{if count == total_results}...{/if} 

which means you can do things like deliver a design element (say, a divider) to all entries except for the last one, or wrap the whole list of what’s returned with some code.

pablo06:37 on 06.01.2009

Good tip Christie

JonnyUK09:47 on 06.01.2009

Correction: The syntax for {total_entries} is incorrect AFAIK, this should actually be {total_results}.

Also worth providing an example - this can be very useful for creating lists such as the following:

{exp:weblog:entries}
{if “{count}” == “1”}<ul>{/if}
<li>{title}</li>
{if “{count}” == “{total_results}”}</ul>{/if}
{/exp:weblog:entries}

Otherwise if you place your <ul> tags outside of the entries tag, you could end up with a list with no list items which is not valid.

Christy Collins15:03 on 06.01.2009

You’re absolutely right JonnyUK! Since it’s not documented, I always have trouble remembering it myself.  I have verified that I’ve used it without brackets and quotes and it works fine that way.  I’ve edited my original post.

moogaloo10:51 on 10.03.2009

very interesting… i guess by using this you could drop a closing div in there with a opening div with display: none in it after say, 2 comments, so you only show the most recent 2.

You can then use a jquery / js “read more…” link to reveal the rest of them, similar to the way facebook only shows the most recent 2 comments to a status by default, but you can expand it to read the whole thread?

I was trying to do something like this but just couldn’t work out how to drop those div tags in there to cut the comment thread in half at a specific point.

genius!

Fabian Socarras15:59 on 10.07.2009

How would you go about wrapping a div tag around every three entries for a total of 12? At the moment I’m having to rely on multiple weblog entry tags, combined with the offset parameter.