smarty中显示数组元素

发布于 2024-12-07 03:32:55 字数 1727 浏览 1 评论 0原文

我合并了两个 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" 
    }
}

我应该在 smarty 中显示它们,所以:

assign_values('rand_ad',$resN);

现在我尝试在 smarty 中显示,如下所示:

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

它显示结果,但按顺序显示。我需要提取某些位置的值。那么如何提取值,例如名字、姓氏等?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-12-14 03:32:55

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

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}

如果我假设不正确,请使用您期望更清晰的输出更新您的问题。

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.

芯好空 2024-12-14 03:32:55

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

在 php 文件中:

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

它将起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文