0

I've been spent lots of time in this multiple selected array which is hold several values in a multiple dropdown box, and what I want is insert selected values into table.

Let say I been selected 1,2,3 from dropdown box, when I print_r($this->input->post('category'))`, its shown

Array ( [0] => 1 [1] => 2 [2] => 2 )

however when insert into table, its only has the last value is inserted instead of all 3 values.

Here is View to select several values:

$category = array(
    'name' => 'category',
    'id' => 'category'
);

<select name="category[]" id="<?php echo $category['id'] ?>" multiple="multiple">
                    <?php
                    foreach($catOpts as $catOpt)
                    {
                        $selected = ($this->input->post('category')==$catOpt->category_name) ? 'selected' : '';
                        echo '<option value="'.$catOpt->category_id.'" '.$selected.'>'.$catOpt->category_name.'</option>';
                    }
                    ?>
                </select>

In Controller, I pass values to validation and if validation valid,:

$this->form_validation->set_rules('category[]', 'Category', 'required');

if($this->form_validation->run()) { // validation ok

    if(!is_null($data = $this->db_model->save_listing(          
        $this->form_validation->set_value('category[]')
    ))) { // success

    //some message to acknowledge success created.

    }
}

Model to insert into table:

function save_listing($category)
{

    $data = array(
        'category_id' => $category
    );

    $this->db->insert('listing', $data);

    return TRUE;

}

I do not know how to passes all values (array) into controller $this->form_validation->set_value('category[]') and then perform model function save_listing() and save all values into its column in database.

Please help to solve my problem and I has been browse through lots of forum but no luck to get solution.

Thanks.

1 Answer 1

0

When your field is an array you have to:

$data= array();

while( $v = $this->form_validation->set_value("field[]") )
{
    $data[] =  $v;
}

if you don't, it return the last value.

You also can get the values by $this->input->post('fields') but your sanities rules will not be applied to the values, like htmlspecialchars.

Of course, that's not specified into the doc, like other things..

Source /system/libraries/Form_validation.php:

/**
 * Get the value from a form
 *
 * Permits you to repopulate a form field with the value it was submitted
 * with, or, if that value doesn't exist, with the default
 *
 * @access  public
 * @param   string  the field name
 * @param   string
 * @return  void
 */
public function set_value($field = '', $default = '')
{
    if ( ! isset($this->_field_data[$field]))
    {
        return $default;
    }

    // If the data is an array output them one at a time.
    //     E.g: form_input('name[]', set_value('name[]');
    if (is_array($this->_field_data[$field]['postdata']))
    {
        return array_shift($this->_field_data[$field]['postdata']);
    }

    return $this->_field_data[$field]['postdata'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.