2

I want to load this array as table in Smarty.

Array:

Array
(
    [0] => Array
        (
            [name] => VS1
            [price] => 350
            [ram] => 256
            [cpu] => 2267
            [hdd] => 5
            [traff] => 0
            [os] => Linux
            [country] => Russia
        )

    [1] => Array
        (
            [name] => VS2
            [price] => 465
            [ram] => 512
            [cpu] => 2267
            [hdd] => 5
            [traff] => 0
            [os] => Linux
            [country] => Russia
        )
)

In source, i want this

    <tr>
                    <td>VS1</td>
                    <td>350</td>
                    <td>256</td>
                    <td>2267</td>
                    <td>5</td>
                    <td>0</td>
                    <td>Linux</td>
                    <td>Russia</td>
    </tr>

    <tr>
                    <td>VS2</td>
                    <td>465</td>
                    <td>512</td>
                    <td>2267</td>
                    <td>5</td>
                    <td>0</td>
                    <td>Linux</td>
                    <td>Russia</td>
    </tr>

Is this possible? I am trying foreach cycles, but its no result. Can you give me a working Smarty code please?

3 Answers 3

10

foreach always worked for me. lets asume, $smarty is your already initialized and working smarty 3 instance, and $arr is your array.

you need to assign the array to smarty in php:

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

then you loop through it with foreach in the template:

{foreach $arr as $item}
    <tr>
        <td>{$item.name}</td>
        <td>{$item.price}</td>
        <td>{$item.ram}</td>
        ....
    </tr>
{/foreach}
Sign up to request clarification or add additional context in comments.

Comments

2

This should do the trick ;)

{foreach from=$myArr item="row"}
    <tr>
        {foreach from=$row item="col"}
            <td>{$col}</td>
        {/foreach}
    </tr>
{/foreach}

Comments

1
{foreach from=$myArray item=foo}
    <li>{$foo}</li>
{/foreach}


{foreach $arr as $item}
{/foreach}

Mind the other way of writing... check your Smarty version for the correct one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.