Display array elements in smarty

夙愿已清 提交于 2019-12-31 05:29:03

问题


I have merged two mysql results :

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) shows the following result :

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" 
    }
}

I am supposed to display them in smarty so :

assign_values('rand_ad',$resN);

Now I tried to display in smarty like this :

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

It displays the results but serially. I need to extract the values in some positions . So how can I extract the values eg first name, last name etc ?


回答1:


You didn't give an example output of what you wanted, so I'll take a guess. Assuming you are looking for the following output for the array above:

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

You'd could do this:

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

If I am assuming incorrectly, please update your question with the output you are expecting to be more clear.




回答2:


Use {foreach} to display array in smarty. and for database connection use ADODB.

In php file:

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

It will work.



来源:https://stackoverflow.com/questions/7609929/display-array-elements-in-smarty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!