CodeIgniter Forums
drop downs, array syntax name, re-population - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: drop downs, array syntax name, re-population (/showthread.php?tid=30556)



drop downs, array syntax name, re-population - El Forum - 05-18-2010

[eluser]theprodigy[/eluser]
My apologies if this has already been asked, but a quick search brought no results.

I have a drop down on my form, that beside that drop down is a '+' link that uses javascript to add another duplicate drop down. These drop downs have an array syntax name (like "actor[]").

I'm trying to figure out the best way to re-populate the drop downs when the form is re-shown due to a form error.

Does anyone have any good ideas?


drop downs, array syntax name, re-population - El Forum - 05-18-2010

[eluser]pickupman[/eluser]
You have to do a foreach loop and echo out each option element.
Code:
<select name="actor">
&lt;?php foreaach($actor as $item):?&gt;
  
   <option value="&lt;?=$item['value'];?&gt;" &lt;?=set_select('actor',$item['value']);?&gt; >&lt;?=$item['name'];?&gt;</option>

&lt;?php endforeach;?&gt;
</select>



drop downs, array syntax name, re-population - El Forum - 05-18-2010

[eluser]theprodigy[/eluser]
I'm not talking about the number of options in a select, I'm talking about a dynamic number of selects.

Take a look at [link removed] to see what I'm talking about.

When you get there, click the + link next to actors. Another complete drop down will show up. When this is submitted, it is submitted as an array, because of the name of the elements ('actors[]').

If when the form is submitted, there is an error, and I need to repopulate the form, I'm trying to figure out the best way to tell how many selects to reproduce, and how to properly set each one.


drop downs, array syntax name, re-population - El Forum - 05-18-2010

[eluser]theprodigy[/eluser]
ok, I think I got it. If there is a more effecient way of doing it, I'm all ears, but here is how I did it.
Controller:
the name of the element is 'actors[]', but I need to grab it by 'actors' (without the [])
Code:
$this->data['actors_submitted'] = array();
if($this->input->post('actors'))
{
    $this->data['actors_submitted'] = $this->input->post('actors');
}
View:
Code:
&lt;?php
            if(! empty($actors_submitted))
            {
                $id = 1;
                foreach($actors_submitted as $actor)
                {
                    echo form_dropdown('actors[]', $actors, $actor, 'id="actor_' . $id . '"');
                    if($id == 1)
                    {
                        echo '<a href="#">+</a>';
                    }
                    if($id < count($actors_submitted))
                    {
                        echo '<br />';
                    }
                    $id++;
                }
            }
            else
            {
                echo form_dropdown('actors[]', $actors, '', 'id="actor_1"') . '<a href="#">+</a>';
            }
        ?&gt;