Welcome Guest, Not a member yet? Register   Sign In
Store multiple values from select field in DB
#1

[eluser]heaversm[/eluser]
I've got an HTML form which allows a user to select multiple options in a dropdown. I then pass that data on as post data to a CI model. How do I store the multiple values in CI? This is my current code (which only stores the last of the multiple selected values:

Code:
function edit_member()
{
    if (isset($_POST['submit'])) {
        $v_id = $this->input->post('edit_id');
        //$v_memberdep = $this->input->post('sel_dep'); //ONLY STORES ONE VALUE
    }

$data = array(
    'member_dep' => $v_memberdep
);

$this->db->where('id', $v_id);
$this->db->update('members', $data);

}
#2

[eluser]cideveloper[/eluser]
you need to pass $this->input->post('sel_dep') as an array

Code:
<select name="sel_dep[]"  multiple="multiple">
<option value="1">simpletest 1</option>
<option value="2">simpletest 2</option>
<option value="3">simpletest 3</option>
<option value="4">simpletest 4</option>
<option value="5">simpletest 5</option>
</select>

I dont really know how you intend on storing the multiple values in the db. Do you have only one field to store all the info? are you going to serialize the array and store it that way?
#3

[eluser]heaversm[/eluser]
Yes(?) - I don't have a lot of experience in this, but what I was assuming was that the comma delimited list of items would go into one field, and upon retrieval from the DB, I would parse that list out as separate items for display in the dropdown.
#4

[eluser]cideveloper[/eluser]
You can use the below functions to serialize the form data, then load to filed in db and unserialize the db data and use the array in your forms

Code:
/**
     * Serialize an array
     *
     * This function first converts any slashes found in the array to a temporary
     * marker, so when it gets unserialized the slashes will be preserved
     *
     * @access    private
     * @param    array
     * @return    string
     */
    function _serialize($data)
    {
        if (is_array($data))
        {
            foreach ($data as $key => $val)
            {
                $data[$key] = str_replace('\\', '{{slash}}', $val);
            }
        }
        else
        {
            $data = str_replace('\\', '{{slash}}', $data);
        }

        return serialize($data);
    }

    // --------------------------------------------------------------------

    /**
     * Unserialize
     *
     * This function unserializes a data string, then converts any
     * temporary slash markers back to actual slashes
     *
     * @access    private
     * @param    array
     * @return    string
     */
    function _unserialize($data)
    {
        $data = @unserialize(strip_slashes($data));

        if (is_array($data))
        {
            foreach ($data as $key => $val)
            {
                $data[$key] = str_replace('{{slash}}', '\\', $val);
            }

            return $data;
        }

        return str_replace('{{slash}}', '\\', $data);
    }
#5

[eluser]heaversm[/eluser]
Cool thanks. So one last question, in my model, do I refer to the field still as sel_dep, or must it now be sel_dep[] ?

In other words, $v_memberdep = $this->input->post('sel_dep[]');

Oh, also, I want to replace commas, right, not slashes?




Theme © iAndrew 2016 - Forum software by © MyBB