开发者

Smarty and array

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?


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}


This should do the trick ;)

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


{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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜