Count items

  Previous article Next article  

Count items

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 1.11.12
  Last updated: 13-01-2015
  Comments: 0

Small User Defined Tag that count items in an array, useful for several modules like News, CGBlog, Products, etc.
This article originates from the former CMS Made Simple Wiki, the original author is Elijah Lofgren.

Note: The following Smarty tag has the same functionality! I post this UDT anyway because it might be a inspiration help to you for other purposes...

Alternative method
{$itemcount = $items|@count}

Create a new User Defined Tag, named "count_items"

count_items
$items = cmsms()->GetSmarty()->get_template_vars('items');
$smarty->assign('itemcount', count($items));

  How to use

Template example
{count_items}
{if $itemcount}
     <p>There are {$itemcount} items on this website</p>
     <!-- start normal template -->
     {foreach from=$items item=entry}

     {/foreach}
     <!-- end normal template -->
{else}
     <p>There are no items...</p>
{/if}

  Working example

CMSMS Troubleshooting information database


A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  0 Comments

No comments yet...

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Count items

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 1.11.12
  Last updated: 13-01-2015
  Comments: 0

A2 Webhosting CMS Made Simple