Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

[Solved] How do I generate select form from a multiple array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
waro
Smarty Regular


Joined: 12 Jun 2008
Posts: 38

PostPosted: Wed Jan 27, 2016 2:50 am    Post subject: [Solved] How do I generate select form from a multiple array Reply with quote

Hi,
If I have an array like below:
Code:
Array
(
    [0] => Array
        (
            [form_type_id] => 1
            [form_title] => Full Name
            [form_type] => text
            [values] => 255
            [selections] =>
        )

    [1] => Array
        (
            [form_type_id] => 14
            [form_title] => Email
            [form_type] => text
            [values] => 255
            [selections] =>
        )

    [2] => Array
        (
            [form_type_id] => 54
            [form_title] => Test Types
            [form_type] => select
            [values] => TOEFL,IELTS
            [selections] => Array
                (
                    [0] => TOEFL
                    [1] => Array
                        (
                            [selection] => IELTS
                        )

                    [] => Array
                        (
                            [selection] => TOEFL
                        )

                )

        )

    [3] => Array
        (
            [form_type_id] => 58
            [form_title] => Degrees
            [form_type] => select
            [values] => High School, Bachelor, Master, Doctor
            [selections] => Array
                (
                    [0] => SMA
                    [1] => S1
                    [2] => Array
                        (
                            [selection] => SMA
                        )

                    [3] => Array
                        (
                            [selection] => S1
                        )

                    [4] => Array
                        (
                            [selection] => S2
                        )

                    [5] => Array
                        (
                            [selection] => S3
                        )

                )

        )

)

How do I get a template to have an HTML form like below:
Code:
 <form method="post" action="process.php">
  Full name: <input type="text" name="1">
  Email: <input type="text" name="14">
  Test type:
  <select name="54">
      <option value="TOEFL">
      <option value="IELTS">
  </select>
  Degree:
  <select name="58">
      <option value="High School">
      <option value="Bachelor">
      <option value="Master">
      <option value="Doctor">
   </select>
   <submit>
</form>

I tried using nested {section} below but the option value are empty:
Code:
{section name=ft loop=$form_types}
    {$form_types[ft].form_title}
      {if $form_types[ft].form_type eq 'select'}<select name="{$form_types[ft].form_type_id}">{/if}
      {if $form_types[ft].form_type eq 'date'}
          <input type="text" name="{$form_types[ft].form_type_id}">
          {elseif $form_types[ft].form_type eq 'file'}
          <input type="file" name="{$form_types[ft].form_type_id}">
          {elseif $form_types[ft].form_type eq 'select'}
  {section name=fs loop=$form_types[$selections]}
            <option value="{$form_types[$selections][fs].selection}"> {$form_types[$selections][fs].selection}
  {/section}
          {elseif $form_types[ft].form_type eq 'text'}
          <input type="text" name="{$form_types[ft].form_type_id}">
          {elseif $form_types[ft].form_type eq 'textarea'}
          <textarea name="{$form_types[ft].form_type_id}" rows="11"></textarea>
          {/if}
      {if $form_types[ft].form_type eq 'select'}</select>{/if}
{/section}

Thank you in advance.


Last edited by waro on Wed Jan 27, 2016 1:46 pm; edited 1 time in total
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jan 27, 2016 10:48 am    Post subject: Reply with quote

1. {foreach}
2. There's option list builder tag.
Back to top
View user's profile Send private message
waro
Smarty Regular


Joined: 12 Jun 2008
Posts: 38

PostPosted: Wed Jan 27, 2016 1:45 pm    Post subject: Reply with quote

Thank you, you're a life saver.. Both answers are correct
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP