How to insert data into database from multiple dropdowns in codeigniter?

I have a drop down that looks like this

<select class="selectpicker form-control" multiple name="category[]">
    <option value="1" id="MENTAL HEALTH">MENTAL HEALTH</option>
    <option value="2" id="SUICIDE SUPPORT">SUICIDE SUPPORT</option>
    <option value="3" id="HEALTH">HEALTH</option>
    <option value="4" id="SUPPORT">SUPPORT</option>
</select>

      

In my controller, I have the following code to get this data from the dropdown menu

public function save()
{
    $ctgry= ($this->input->post('category'));
    $orgName= ($this->input->post('InputName'));
    $streetNo= ($this->input->post('StreetNo'));
    $streetName= ($this->input->post('StreetName'));
    $suburb= ($this->input->post('Suburb'));
    $state= ($this->input->post('state'));
    $postCode= ($this->input->post('PostCode'));
    $count = count($ctgry);

    $supprtGroup = array(
        'name' => $orgName,
        'street_no' => $streetNo,
        'street' => $streetName,
        'suburb' => $suburb,
        'state' => $state,
        'pc' => $postCode
    );

    $supportgroup_id = $this->Home_Model->addSupportGroup($supprtGroup);

    $j = 0;
    i = 0;
    $sc_id = 0

    echo 'aa';//just for error checking purpose

    if ($count > 1)
    {
        echo ' aa1';//just for error checking purpose
        while ($i > $count)
        {
            echo ' aa2';//just for error checking purpose

            $support_category = array(
                'supportgroup_id' => $supportgroup_id,
                'category_id' => $ctgry[$j]
            );
            $sc_id = $this->Home_Model->addSupportrCategory($support_category);
            $i++;
            echo $sc_id;//just for error checking purpose

        }
    }
    else
    {
        echo ' aa3';//just for error checking purpose
        $support_category = array(
            'supportgroup_id' => $supportgroup_id,
            'category_id' => $ctgry[$j]
        );
        $sc_id = $this->Home_Model->addSupportrCategory($support_category);
        echo $sc_id;//just for error checking purpose
    }
    echo'bb';//just for error checking purpose
}

      

Below is my model class

function addSupportGroup($supprtGroup=NULL)
{
    $this->db->insert('support_group', $supprtGroup);
    return $this->db->insert_id();
}

function addSupportrCategory($support_category=NULL)
{
    $this->db->insert('support_category', $support_category);
    return $this->db->insert_id();
}

      

When you select multiple values ​​from the dropdown do the following

aa aa1bb

      

Can anyone give me an answer as to why this is happening, if this one value is selected from the drop-down list, saving it to the DB is fine, I search all similar ones but could not find an answer.

+3


source to share


1 answer


I don't see where it is $count

asked in your question, so I don't know what value it is intended for, however if you get "aa1" as output, you say it is greater than 1.

With yours, while()

you say it $i

should be greater than $count

, but $ i is 0, so the while loop doesn't start.

If you just want to iterate over the categories it would be easier:

$supportgroup_id = $this->Home_Model->addSupportGroup($supprtGroup);

foreach ($ctgry as $cat) {
    $insert = array(
       'supportgroup_id' => $supportgroup_id,
       'category_id' => $cat 
    );

    $sc_id = $this->Home_Model->addSupportrCategory($support_category);
}

      



This will work because the dropdown will always be selected as an array.

You should also look at adding some validation to this!

Hope this helps!

+1


source







All Articles