Loading...
 
Smarty and Templates

Smarty and Templates


[Solved] How to count the number of items from an item link tracker field

Israel

It may help someone else and can be integrated in Tiki after improvement.

I have a case in a tracker where an item can be attached to one or more children assigned using a item link tracker field.
In some case I have one child selected in the item link field, sometimes two or three.
I needed to output the total number of children for those item (filtered) in a smarty template using a foreach.

To do this I’m counting the number of "commas" in the string.

IE:
$row.tracker_field_productsboughtChildName_text : Bernard (0)
$row.tracker_field_productsboughtChildName_text : Marc,Jonny (1)
$row.tracker_field_productsboughtChildName_text : Gary,Luci,Jean Marc (2)

In my output template I use the following to count on each foreach and add to the $sum_all_child

{foreach from=$results item=row}
	{if $row.tracker_field_productsboughtChildName_text} {* we check the string is not empty *}
		{$count = $row.tracker_field_productsboughtChildName_text|substr_count:','} {* counting the number of commas in the string *}
		{assign var="number" value=$count + 1} {* we start from 0 if 1 item, no comma, so we add 1 to number *}
		{assign var="sum_all_child" value=$sum_all_child+$number} {* each foreach we add the number to the sum of all children *}
	{/if}
{/foreach}
{assign var="totalChild" value=$sum_all_child} {* we create a variable and assign to it the value calculated in a different scope *}
{$totalChild} {* we display the variable - the total of items in the item link  tracker_field_productsboughtChildName *}


It will be nice to have it improved (more generic) and useable overall in Tiki (item link, item list and dynamic item link)

Important note: you need to go "tiki-admin.php?page=security" and add "substr_count" in the Extra Smarty modifiers field.
See the following links for more details about this :