Answer the question
In order to leave comments, you need to log in
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);
{$data|@count}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question