x
 
Create New Page
 View Previous Changes    ( Last updated by CDT )

Count Entries

Question:

Is it possible to get the number of entries in a set of entries?

Answer:

<head
<?php $entry_count=0?> <!-- initialize a variable --> 
</
head

<
body
<
div id="maincontent"
{exp:weblog:entries} 
<?php $entry_count
++; ?> <!-- increment the variable --> 
{title} 
{
/exp::weblog:entries} 
</div

<
div id="sidebar"
<
p>This page lists <?php echo($entry_count); ?> entries</p> <!-- output the count --> 
</
div
</
body

Basically, this will count the number of entries are being displayed on the page. That’s all.

This technique can of course be used to do things like number comments or entries as well (just use the entry_count variable inside the exp:weblog:entries block), and I’m sure this has been mentioned elsewhere in these forums. I’ve never seen mention of a simple counter being used as an entry tally, though, so I put it here in the event someone does a forum search for entry count.

The biggest downside of this method is that you can only use the tally after the exp:weblog:entries block, but it gets the job done rather simply otherwise.

Is it possible to show different results, based on the count?

If you want to evaluate the results of the count and show one thing if the result is 0, another if it is 1, and yet another if it is more than 1.

(There may be a better way to do this but this worked.)

Second Answer 2:

<?php $entry_count=0?> <!-- initialize a variable -->
{exp:weblog:entries weblog="weblogname" author_id="CURRENT_USER" dynamic="off" disable="categories|trackbacks|pagination|category_fields" rdf="off"  status="open"}
<?php $entry_count
++; ?> <!-- increment the variable --> 
{/exp:weblog:entries}

{if <?php 
echo($entry_count); ?> == "0" }
There are no entries
.  You need to add one.
{/if}

{if <?php 
echo($entry_count); ?> == "1" }
<?php 
echo($entry_count); ?> Entry
{
/if}

{if <?php 
echo($entry_count); ?> "1" }
<?php 
echo($entry_count); ?> Entries
{
/if} 

Category:Tricks
Category:PHP

Category:EE1

Categories: