0

I am trying to make the following loop work. Basically, I am trying to display the children as options. Why doesn't it work? The optiongroups are being displayed. And the arrays are constructed the right way.

{foreach from=$tpl_parents item='row' key='i'}
    <optgroup label="{$row.NAME}">
        {foreach from=$tpl_children.$i item='child' key='y'}
            <option value="{$y}">{$child.VALUE}</option>
        {/foreach}
    </optgroup>
{/foreach}

The array I am trying to loop through is constructed this way: Parent array:

array(328) {
 [0]=>
   array(42) {
    ["ID"]=>
     string(4) "123"
    ["NAME"]=>
     string(6) "blabla"
    ...
    ...

Child array:

array(192) {
  [123]=>
    array(2) {
      [881]=>
        array(11) {
            ["CHILD_ID"]=> string(5) "881"
            ["PARENT_ID"]=> string(4) "123"
            ["VALUE"]=> string(2) "No"
    ...
    ...
1
  • If you weren't using Smarty, I'd suggest using a RecursiveIterator... Commented Aug 5, 2010 at 16:59

1 Answer 1

3

Looks like you need to nest another foreach in there to get the actual child item array:

{foreach from=$tpl_parents item='row' key='i'}
    <optgroup label="{$row.NAME}">
        {foreach from=$tpl_children.$i item='child' key='j'}
            {foreach from=$child item='child_item' key='y'}
                <option value="{$y}">{$child_item.VALUE}</option>
            {/foreach}
        {/foreach}
    </optgroup>
{/foreach}

It is a bit hard to work out as the arrays you have provided are not complete and do not have their variable names associated with them. eg. $row = array('blah');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.