D
D
Denis Davydenko2018-09-03 15:07:55
Smarty
Denis Davydenko, 2018-09-03 15:07:55

How to count the number of elements in an array with a specific value in Smarty without new assigns?

Good afternoon.
PHP:

$query = "SELECT * FROM `statistic` ORDER BY `nickname`;";
$result = $mysqli->query($query);
$data = [];
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    $data[] = $row;
}

$smarty->assign('data', $data);

By googling, I found the @count function in Smarty 3.
{$data|@count}
In this way, it counts the total number of rows.
Are there any modifiers/conditions/crutches to use only Smarty tools to count the number of lines with a certain value (For example: {$data.A == 'YES'}. It is important not to use new variables in PHP.
PS: For lovers of lyrical digressions I'm learning Smarty because I want to learn Smarty PHP/Twig is better but I want to learn Smarty So I'm making it harder (without introducing new variables in PHP) because I'm learning Smarty not PHP and want to solve ( if possible) the task exclusively by means of Smarty.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2018-09-03
@GooseTheDestroyer

1. Make your own method count_with($field)
2. Register it as a plugin https://www.smarty.net/docs/en/api.register.plugin.tpl
3. Use it like this: {$data|count_with:'YES'}
and that's it :)
This is purely a Smarty solution -- registration plugin, that is, a custom PHP function, that is, in this way you will CORRECTLY use Smarty (in other words, GOOD AND TRUE to learn it) Because
this is exactly its purpose - in it (in Smarty) they specially came up with such a tool for CORRECT use of the functionality, because it is a template engine 100%, so you template the template ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question