duanjiu6697 2017-10-22 15:26
浏览 133
已采纳

从Smarty中的数组中抓取某些值

I have been trying this for hours, If it was just PHP I would be done by now but this requires Smarty 3 so things are a little different. I am having difficulty grabbing specific keys plural from an Array. The Array looks like this

Array
(
    [0] => Array
        (
            [id] => 1
            [client] => Jane Doe
            [email] => jane@doe.com
        )
   [1] => Array
        (
            [id] => 2
            [client] => John Doe
            [email] => john@doe.com
        )
   [2] => Array
        (
            [id] => 3
            [client] => Jim Doe
            [email] => jim@doe.com
        )

I can access this using PHP just fine, the Smarty is tripping me up, the files are two

  • clients.php
  • clients.tpl <- smarty

I assign the array in the .php file with the following

$totalEntries = $results['products']['product'];
$ca->assign('innerArray', $totalEntries);

The $results['products']['product'] is what outputs the array seen above.

Now in the .tpl file, I have the following

  <select class="form-control" id="sel1">
   {foreach $innerArray as $results} 
    {foreach from=$results.client item=label}
    <option value="{$label}">{$label}</option>
    {/foreach}
  {/foreach}
  </select>

This works to output to the dropdown

  • Jane Doe
  • John Doe
  • Jim Doe

I got that part right, and I have been looking all over the internet to figure this out. My plan was to introduce into the drop down something like

  • Jane Doe - jane@doe.com
  • John Doe - john@doe.com
  • Jim Doe - jim@doe.com

However when I attempt this using something like the following where i remove the .client part of the from=

  <select class="form-control" id="sel1">
   {foreach $innerArray as $results} 
    {foreach from=$results item=label}
    <option value="{$label.client}">{$label.client} - {$label.email}</option>
    {/foreach}
  {/foreach}
  </select>

I am met with a list that looks like this

  • 1 - 1
  • J - J
  • j - j
  • 2 - 2
  • J - J
  • j - j
  • 3 - 3
  • J - J
  • j - j

I realize this is basically the first letters and numbers, but I see variety of examples online showing that I can take from the array what I need, but when I try $label.client - $label.email It won't work.

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doufangzhang4454 2017-10-22 15:43
    关注

    It's not like the way you did, but using {section} will do:

    <select class="form-control" id="sel1">
    {section name=seq loop=$innerArray}
      <option value="{$innerArray[seq].id}">{$innerArray[seq].client} - $innerArray[seq].email}</option>
    {/section}
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题