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);

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

            foreach($selections as $selection)
                      'selection' => $selection

            'id'        => $rows['value'],
            'form_name' => $rows['form_name'],
            'form_type' => $rows['form_type'],
            'value'     => $rows['value']


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}

    {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
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
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.