Problem with nested {section} in PHP Smarty

I’m trying to build a form builder but I have one problem, I can’t work the nested section.

Here’s my simplified data table:

+---+--------------+------------+-----------------------------------
| id | form_name   | form_type  | value 
+---+--------------+------------+-----------------------------------
| 1  | Full Name   | text       |
| 2  | Sex         | select     | 'm','f','other'
| 3  | Birthday    | text       |
| 4  | Country     | select     | 'UK','USA','France','Australia'
| 5  | Photo       | file       |
+---+--------------+------------+-----------------------------------

Here’s my PHP code:

$sql = "SELECT * FROM table";
$form_types = $DB->($sql);
$i=0;

if (isset($form_types))
{
    foreach($form_types as $rows)
    {
        if (strpos($rows['value'], ','))
        {
            $selections = explode(",", $rows['value']);
            $j=0;

            foreach($selections as $selection)
                {
                   $selections[$j++]=array(
                      'selection' => $selection
                   );
                }
            }

        $form_types[$i++]=array(
            'id'        => $rows['value'],
            'form_name' => $rows['form_name'],
            'form_type' => $rows['form_type'],
            'value'     => $rows['value']
        );
    }
}

$tpl->assign('selections',$selections);
$tpl->assign('form_types',$form_types);

And here’s my template:

{section name=ft loop=$form_types}
    {if $form_types[ft].form_type eq 'select'}{$form_types[ft].form_name}  <select name="{$form_types[ft].id}">{/if}

    {if $form_types[ft].form_type eq 'text'}
          {$form_types[ft].form_name}  <input type="text" name="{$form_types[ft].id}">
        {elseif $form_types[ft].form_type eq 'file'}
          {$form_types[ft].form_name}  <input type="file" name="{$form_types[ft].id}">
        {elseif $form_types[ft].form_type eq 'select'}
          {section name=fs loop=$selections[ft]}
            <option value="{$selections[ft][fs].selection}"> {$selections[ft][fs].selection}
          {/section}
        {/if}

    {if $form_types[ft].form_type eq 'select'}</select>{/if}

Where I want the result is:

Full Name <input type="text" name="1">
Sex <select name="2">
    <option value="m"> m
    <option value="f"> f
    <option value="other"> other
    </select>
Birthday <input type="text" name="3">
Country <select name="4">
          <option value="UK"> UK
          <option value="USA"> USA
          <option value="France"> France
          <option value="Australia"> Australia
        </select>
Photo <input type="text" name="5">

But I can’t generate the select tag for ‘Sex’ and ‘Country’.

first thing were to check if your PHP array contains the structure/values you expect.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.