Answer the question
In order to leave comments, you need to log in
What is the correct way to use foreach in Smarty?
Hello everyone, help me understand.
$smarty = new Smarty();
$smarty->assign('phonebook', $phonebook);
$smarty->display('main.tpl');
{foreach from=$phonebook item=personal}
<tr>
<td><a href="profile.php" target="_blank">{$personal.name}</a></td>
<td>{$personal.branch}</td>
<td>{$personal.position}</td>
<td>{$personal.number}</td>
<td>{$personal.email}</td>
</tr>
{/foreach}
Answer the question
In order to leave comments, you need to log in
Everything is simple.
Let's say there is such an array.
$phonebook = [
['name' => 'Фёдор Михалыч', 'phone' => '118444', 'email' => '[email protected]'],
['name' => 'Пётр Иваныч', 'phone' => '218510', 'email' => '[email protected]']
];
{foreach from=$phonebook item=person}
Имя - {$person.name}; Тел. - {$person.phone}; Почта - {$person.email};
{/foreach}
name
, phone
, etc., there will be numeric indices 0
, 1
, 2
... <table>
{foreach $array as $k => $v }
<tr>
{foreach $v as $k1 => $v1 }
<td>{$v1}</td>
{/foreach}
</tr>
{/foreach}
</table>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question