D
D
Denis Davydenko2018-03-30 17:35:56
Smarty
Denis Davydenko, 2018-03-30 17:35:56

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}

This code displays such an abra-kadabra (there are 2 records in the database):
5abe4af7d7e6c128668185.png
Actually the question is CHADNT?
PS: I read the documentation, but it's hard to understand without an example.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-03-30
@GooseTheDestroyer

Everything is simple.
Let's say there is such an array.

$phonebook = [
   ['name' => 'Фёдор Михалыч', 'phone' => '118444', 'email' => '[email protected]'],
   ['name' => 'Пётр Иваныч', 'phone' => '218510', 'email' => '[email protected]']
];

The code for "jogging" on it will be like this.
{foreach from=$phonebook item=person}
Имя - {$person.name}; Тел. - {$person.phone}; Почта - {$person.email};
{/foreach}

If the array is non-associative, then instead of name, phone, etc., there will be numeric indices 0, 1, 2...
PS Documentation .

S
Sergey Baklanov, 2018-05-03
@php_cat_com

<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 question

Ask a Question

731 491 924 answers to any question