在smarty中显示数组元素

时间:2011-09-30 11:51:37

标签: php foreach smarty

我合并了两个mysql结果:

while($rs_1 = mysql_fetch_array($r1))
{
    $arr1[] = $rs_1;
}

while($rs_2 = mysql_fetch_array($r2))
{
    $arr2[] = $rs_2;
}

$resN = array_merge($arr1,$arr2);

var_dump($resN)显示以下结果:

array(5) { 
    [0] => array(4) { 
        [0] => string(6) "Petric" 
        ["bz_pro_first_name"] => string(6) "Petric" 
        [1] => string(8) "Naughton" 
        ["bz_pro_last_name"] => string(8) "Naughton" 
    } 
    [1] => array(4) { 
        [0] => string(6) "Nitish" 
        ["bz_pro_first_name"] => string(6) "Nitish" 
        [1] => string(12) "Dolakasharia" 
        ["bz_pro_last_name"] => string(12) "Dolakasharia" 
    } 
    [2] => array(4) { 
        [0] => string(6) "Martin" 
        ["bz_pro_first_name"] => string(6) "Martin" 
        [1] => string(3) "Rom" 
        ["bz_pro_last_name"] => string(3) "Rom" 
    } 
    [3] => array(4) { 
        [0] => string(5) "Steve" 
        ["bz_pro_first_name"] => string(5) "Steve" 
        [1] => string(5) "Wough" 
        ["bz_pro_last_name"] => string(5) "Wough" 
    } 
    [4] => array(4) { 
        [0] => string(3) "Liz" 
        ["bz_pro_first_name"] => string(3) "Liz" 
        [1] => string(6) "Hurley" 
        ["bz_pro_last_name"] => string(6) "Hurley" 
    }
}

我应该巧妙地展示它们:

assign_values( 'rand_ad',$ RESN);

现在我试着像这样聪明地展示:

{foreach name=outer item=pro from=$rand_pro}
    {foreach key=key item=item from=$pro}
        {$key}: {$item}<br />
    {/foreach}
{/foreach}

它会连续显示结果。我需要在某些位置提取值。那么如何提取值,例如名字,姓氏等?

2 个答案:

答案 0 :(得分:4)

您没有给出您想要的示例输出,所以我会猜测。假设您正在为上面的数组寻找以下输出:

0: Petric Naughton<br />
1: Nitish Dolakasharia<br />
2: Martin Rom<br />
3: Steve Wough<br />
4: Liz Hurley<br />

你可以这样做:

{foreach from=$rand_pro item=pro key=pro_key}
    {$pro_key}: {$pro.bz_pro_first_name} {$pro.bz_pro_last_name}<br />
{/foreach}

如果我假设错误,请使用您希望更清晰的输出更新您的问题。

答案 1 :(得分:0)

使用{foreach}在smarty中显示数组。 并且对于数据库连接,请使用ADODB

在php文件中:

type $smarty->assign->("arrname",$aodDBconn->Execute($sql)).

它会起作用。