EllisLab text mark
Advanced Search
1 of 2
1
   
Using Arrays as Field Names
Posted: 29 May 2010 09:50 AM
Joined: 2009-09-20
13 posts

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

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

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

 
Posted: 30 May 2010 03:24 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

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.

$this->form_validation->set_rules('options[]','options''trim'); 
 
Posted: 30 May 2010 06:32 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-09-20
13 posts

Hi,
Yes, I have exactly that in my controller.

So here is a little bit more detail about the 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.

 
Posted: 31 May 2010 12:35 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

Just out of curiosity is it showing up correctly in your $_POST array. Put this in your controller

$this->output->enable_profiler(TRUE); 

Submit the form, and see how it is shown.

 
Posted: 31 May 2010 04:21 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-09-20
13 posts

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!!

Array
(
    
[1] => 2010-05-13
    [2] 
=> 2010-05-26
    [3] 
=> 2010-05-18
    [4] 
=> 
    
[5] => 
    
[6] => 
    
[7] => 
    
[8] =>  
 
Posted: 01 June 2010 10:24 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

Guess, I never had tried this before. I tried this and it worked.

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

 
Posted: 19 June 2010 07:12 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-09-20
13 posts

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:

SeverityNotice
Message
Uninitialized string offset
 
Posted: 19 June 2010 11:23 AM   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

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

 
Posted: 19 June 2010 03:18 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-09-20
13 posts

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.

value="<?php if(!empty($options)) echo $options[$n]; ?>" 
 
Posted: 20 June 2010 05:01 PM   [ # 9 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

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.

$options set_value('options[]',array('','','','','')); 
 
Posted: 20 June 2010 06:35 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-09-20
13 posts

That’s much nicer.
Thanks a lot for your help!!

 
Posted: 02 August 2010 06:35 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2009-11-23
9 posts

I found the following bug report that helped solve this issue for me.

Arrays as field names not working

Thanks, zdknudsen!  Worked like a charm.  I didn’t have anything to do with it, but maybe this will help someone save some time and frustration in the future.

 
Posted: 11 September 2010 01:43 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-18
9 posts

Can someone please post some working code.  I’m going out of my mind trying to solve this problem that at least two people have solved here.  Also, the link for the bug tracker with it’s solution is a dead link. 

I keep getting either the word ‘Array’ or blanks in my input fields on return from the server with invalid data.

 
Posted: 13 September 2010 09:58 AM   [ # 13 ]   [ Rating: 0 ]
Joined: 2009-11-23
9 posts

Since the above link is dead, here’s the fix that worked for me.  I take no credit.  It was posted in the bug fix.  This is what the set_value function should look like.  It can be modified in the Form_validation library, but it is recommended to override it in the custom project library MY_Form_validation.  Here goes.

function set_value($field ''$default '')
{
    
if ( ! isset($this->_field_data[$field]))
    
{
        
return $default;
    
}

    $field 
= &$this->_field_data[$field]['postdata'];

    if (
is_array($field))
    
{
        $current 
each($field);
        return 
$current['value'];
    
}

    
return $field;

Hope this helps.

 
Posted: 13 September 2010 06:08 PM   [ # 14 ]   [ Rating: 0 ]
Joined: 2009-11-23
9 posts

To expound on the revision above, the following is the way I have set up the controller and the view.  Set up validation in the controller as follows.

Controller:

...

$this->form_validation->set_rules('item_id[]''Item Id''trim');
$this->form_validation->set_rules('item_no[]''Item No.''trim');
$this->form_validation->set_rules('item_description[]''Item Description''trim');
$this->form_validation->set_rules('item_type[]''Item Type''trim');

... 

item_id is the auto-incremented key value for items in a database and is hidden in the view.  The other three fields are columns in the table and will be fields in the view. 

Then in the view, I chose to dynamically set the field values depending on the existing condition.  I suppose there’s a more elegant way to refactor this code, but I, too, spent a lot of time pulling my hair out.  I was just happy with a solution.

View:

...

<
table id="tblItems">
    <
tr>
    <
td class="txtBase">Item No.</td>
    <
td class="txtBase">Item Description</td>
    <
td class="txtBase">Item Type</td>
    </
tr>

<?php if(isset($_POST['item_no'])) : ?>  <!-- this is to set values for forms that do not pass validation -->
<?php foreach($_POST['item_no'as $row) : ?>
<tr>
    <
input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]'); ?>" />
    <
td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]'); ?>" /></td>
    <
td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]'); ?>" /></td>
    <
td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]'); ?>" /></td>
</
tr>
<?php endforeach; ?>
<?php 
elseif(isset($item_no)) : ?>  <!-- this is to set values for editing forms that have existing item data -->
<?php $i 0?>
<?php 
foreach($item_no as $row) : ?>
<tr>
    <
input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]', $item_id[$i]); ?>" />
    <
td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]', $item_no[$i]); ?>" /></td>
    <
td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]', $item_description[$i]); ?>" /></td>
    <
td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]', $item_type[$i]); ?>" /></td>
</
tr>
<?php $i++; ?>
<?php 
endforeach; ?>
<?php 
else : ?>  <!-- this is to create new form fields if editing/adding a form with no items -->
<
tr>
    <
input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]'); ?>" />
    <
td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]'); ?>" /></td>
    <
td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]'); ?>" /></td>
    <
td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]'); ?>" /></td>
</
tr>
<
tr>
    <
input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]'); ?>" />
    <
td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]'); ?>" /></td>
    <
td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]'); ?>" /></td>
    <
td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]'); ?>" /></td>
</
tr>
<?php endif; ?>

</table>

... 

I’m not sure exactly how others are implementing this “feature.”  I’m using javascript to add blank fields to the form if the user needs to input more data.  That’s a discussion for another time.  Also, writing the post data back to the database is not exactly trivial when dealing with edits and deletions.  Perhaps that will also be a post for another time.  Maybe a wiki is in order for forms such as this.

 
Posted: 13 October 2010 09:33 AM   [ # 15 ]   [ Rating: 0 ]
Joined: 2010-09-14
3 posts

With the fix posted above, default value does not apply when postdata is NULL.
So, a best fix for this issue seem to be :

function set_value($field ''$default ''{
        
if (!isset($this->_field_data[$field])) {
            
return $default;
        
}
        $field 
= &$this->_field_data[$field]['postdata'];
        if(!isset(
$field)) {
            
return $default;
        
}
        
else if (is_array($field)) {
            $current 
each($field);
            return 
$current['value'];
        
}else {
            
return $field;
        
}
    } 
 
1 of 2
1