ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Using Arrays as Field Names

May 29, 2010 9:50am

Subscribe [10]
  • #1 / May 29, 2010 9:50am

    moleculezz

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

  • #2 / May 30, 2010 3:24pm

    pickupman

    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');
  • #3 / May 30, 2010 6:32pm

    moleculezz

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

  • #4 / May 31, 2010 12:35am

    pickupman

    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.

  • #5 / May 31, 2010 4:21am

    moleculezz

    11 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] =>  
    )
  • #6 / Jun 01, 2010 10:24am

    pickupman

    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.

  • #7 / Jun 19, 2010 7:12am

    moleculezz

    11 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:

    Severity: Notice
    Message: Uninitialized string offset: 1
  • #8 / Jun 19, 2010 11:23am

    pickupman

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

  • #9 / Jun 19, 2010 3:18pm

    moleculezz

    11 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]; ?>"
  • #10 / Jun 20, 2010 5:01pm

    pickupman

    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('','','','',''));
  • #11 / Jun 20, 2010 6:35pm

    moleculezz

    11 posts

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

  • #12 / Aug 02, 2010 6:35pm

    bkirkman

    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.

  • #13 / Sep 11, 2010 1:43am

    webdevguy

    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.

  • #14 / Sep 13, 2010 9:58am

    bkirkman

    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.

  • #15 / Sep 13, 2010 6:08pm

    bkirkman

    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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases