CodeIgniter Forums
Using Arrays as Field Names - 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: Using Arrays as Field Names (/showthread.php?tid=30887)

Pages: 1 2 3


Using Arrays as Field Names - El Forum - 05-29-2010

[eluser]moleculezz[/eluser]
Hello,

I have a small problem using arrays as field names. It doesn't seem to work. I checked the user guide and came up with the following method of doing it.
Form validation
Code:
<input type="text" name="options[]" value="<?php echo set_value('options[]'); ?>" size="50" />

When I submit the form. The fields are populated but with the word array.
I also tried the following, but that doesn't populate at all.
Code:
<input type="text" name="options[<?php echo $num;?>]" value="<?php echo set_value('options['.$num.']'); ?>" size="8"  />

I am using a for loop to display multiple input fields.


Using Arrays as Field Names - El Forum - 05-30-2010

[eluser]pickupman[/eluser]
Can we see a little more of your code. This part seems to be correct. Also, make sure you have set a validation rule in your controller for this work.
Code:
$this->form_validation->set_rules('options[]','options', 'trim');



Using Arrays as Field Names - El Forum - 05-30-2010

[eluser]moleculezz[/eluser]
Hi,
Yes, I have exactly that in my controller.

So here is a little bit more detail about the code.
Code:
foreach($ids as $id){
    <input type="checkbox" name="optns[<?php echo $id;?>]" value="<?php echo $id;?>" <?php echo set_checkbox('optns[]', $id);?> />

    <input type="text" name="options[<?php echo $id;?>]" value="<?php echo set_value('options['.$id.']'); ?>" />
}

There are more form elements I am using which are being repopulated as it should. Also the checkbox that I have in the for loop is also repopulating as it should, the text input is what is not repopulating.


Using Arrays as Field Names - El Forum - 05-30-2010

[eluser]pickupman[/eluser]
Just out of curiosity is it showing up correctly in your $_POST array. Put this in your controller
Code:
$this->output->enable_profiler(TRUE);

Submit the form, and see how it is shown.


Using Arrays as Field Names - El Forum - 05-31-2010

[eluser]moleculezz[/eluser]
Yes,
It does output the information. I tried the code you specified.
BTW, didn't know about that command. This is much nicer for debugging. I used to use print_r().
Thanks for the tip!!

Code:
Array
(
    [1] => 2010-05-13
    [2] => 2010-05-26
    [3] => 2010-05-18
    [4] =>
    [5] =>
    [6] =>
    [7] =>
    [8] =>  
)



Using Arrays as Field Names - El Forum - 06-01-2010

[eluser]pickupman[/eluser]
Guess, I never had tried this before. I tried this and it worked.
Code:
<?php
      $options = set_value('options[]');
      $i = 0;
      foreach($ids as $id) : ?>

         <input type="text" name="options[]" id="options_<?=$id;?>" value="<?=$options[$i];?>" />

<?php
      ++$i;
      endforeach; ?>

set_value() returns an 0 indexed array of submitted values. If you set it to a variable, then in your loop it will iterate the values in the array.


Using Arrays as Field Names - El Forum - 06-19-2010

[eluser]moleculezz[/eluser]
Hi.. It took me a while to get back, was kind of busy with exams. But yeah, I tried this code, but I get the following error:

Code:
Severity: Notice
Message: Uninitialized string offset: 1



Using Arrays as Field Names - El Forum - 06-19-2010

[eluser]pickupman[/eluser]
Then change the i=0 to i=1. It looks like you are getting a 1 based indexed array (first array key is 1).


Using Arrays as Field Names - El Forum - 06-19-2010

[eluser]moleculezz[/eluser]
Ok.. so it's working but not quite there..I think I know what the problem is though.

It doesn't recognize the variable until I submit the form. So I get undefined variable error.
Only when I submit and the set_value() passes the array to the variable it works. But when i first load the page, I immediately get undefinded variable error.

I came with a solution which is not that nice, but it works.

Code:
value="<?php if(!empty($options)) echo $options[$n]; ?>"



Using Arrays as Field Names - El Forum - 06-20-2010

[eluser]pickupman[/eluser]
set_value() does take a second parameter. You will probably need to pass an array with the same number of values as there are fields.
Code:
$options = set_value('options[]',array('','','','',''));