Count items

  Previous article Next article  

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"

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

  How to use

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

     <!-- end normal template -->
     <p>There are no items...</p>

  Working example

CMSMS Troubleshooting information database

Buy Me A Coffee

  Comment Form


Click here to open the form



No comments yet...

Buy Me A Coffee

CMS Made Simple - Tutorials, Tips and Tricks - CMSMS

Count items

  Article optimized for CMSMS 2.x

  Last tested in: CMSMS 1.11.12
  Last updated: 23-06-2018
  Comments: 0

Buy Me A Coffee


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!