MENU

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"

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



  Comment Form

Click here to open the form


  0 Comments

No comments yet...

CMS Made Simple - Tutorials, tips and tricks - CMSMS

Count items

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 1.11.12
  Last updated: 18-11-2017
  Comments: 0
  http://cms.ms/Tes8

Advertisement



Ads help me to help you! Thanks!

Ads help me to help you! Thanks!

Ads help me to help you! Thanks!