Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert data to database from multiple select dropdown in codeigniter?

I have drop down which is look like the following

<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 that data from the dropdown

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
}

The following 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 select multiple values from dropdown the out put as follows

aa aa1bb

Could anyone give me an answer why this is happening, if it is a single value selected from the dropdown its saving to the DB fine I search every similar posts but couldn't find the answer.

like image 335
Marius Flevie Avatar asked Jul 02 '26 08:07

Marius Flevie


1 Answers

I can't see where $count is defined in your question so I don't know what it's value is meant to be, however, if you are getting "aa1" as output you are saying that it's bigger than 1.

With your while() you're saying that $i must be bigger than $count but $i equals 0 so therefore the while loop won't start.

If you are just wanting to loop through the categories it would be easier to just:

$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 picked up as an array.

You should also look at adding some validation to this!

Hope this helps!

like image 138
Rwd Avatar answered Jul 03 '26 21:07

Rwd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!