Loading...
 
Features / Usability

Features / Usability


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

posts: 8633 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

Copy to clipboard
{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 :

There are no comments at this time.

Upcoming Events

1)  18 Apr 2024 14:00 GMT-0000
Tiki Roundtable Meeting
2)  16 May 2024 14:00 GMT-0000
Tiki Roundtable Meeting
3)  20 Jun 2024 14:00 GMT-0000
Tiki Roundtable Meeting
4)  18 Jul 2024 14:00 GMT-0000
Tiki Roundtable Meeting
5)  15 Aug 2024 14:00 GMT-0000
Tiki Roundtable Meeting
6)  19 Sep 2024 14:00 GMT-0000
Tiki Roundtable Meeting
7) 
Tiki birthday
8)  17 Oct 2024 14:00 GMT-0000
Tiki Roundtable Meeting
9)  21 Nov 2024 14:00 GMT-0000
Tiki Roundtable Meeting
10)  19 Dec 2024 14:00 GMT-0000
Tiki Roundtable Meeting