2

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?

Star
  • 3,222
  • 5
  • 32
  • 48
Steini Petur
  • 129
  • 1
  • 9
  • Read this: https://stackoverflow.com/questions/19276200/smarty-displaying-only-first-character-from-foreach – David Hope Oct 22 '17 at 15:50
  • That sounds like my issue but not really as I use WHMCS and I am not fetching anything using personal database connection, I am getting already made variables by WHMCS such as $userid and so on. However I arrived at my solution just before the answer came to it after hours of work, but his solution ended up being so much cleaner I dumped mine out the window :) – Steini Petur Oct 22 '17 at 15:59

2 Answers2

2

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>
sianipard
  • 164
  • 2
  • 17
  • Such a beautiful solution.. Thank you so much!, I was almost there but mine was so crummy.. {foreach $innerArray as $results} {foreach from=$results key=key item=label} {if $key == "client"} {/if} {/foreach} {/foreach} – Steini Petur Oct 22 '17 at 15:54
  • Your version, is so clean! mine spit out 100 results until I jammed a "if key" to stop it at certain amount of results.. Accepted hands down. – Steini Petur Oct 22 '17 at 15:56
1

Smarty 3 lets you to use php-style "foreach" but mantains the old Smarty 2 style, so you can simply do the following:

{foreach $arr as $idx => $person}
  <option value="{$person.client}">{$person.client} - {$person.email}</option>
{/foreach}

Also, as per the Smarty 3 manual:

The {foreach} loop can do everything a {section} loop can do, and has a simpler and easier syntax. It is usually preferred over the {section} loop.

Juanga Covas
  • 315
  • 2
  • 5