Jump to content

Store values of checkboxes or Multi selectbox in database table


Recommended Posts

Hi All,

 

Can anyone tell how can I store the values of check boxes or multiselect in a database.

 

I have created a controller and class in admin section and also a table in database.

 

I need it urgent please help me out with this.

 

Thanks in advance :) 

Link to comment
Share on other sites

 

 

Can anyone tell how can I store the values of check boxes or multiselect in a database.

what kind of multiselect you've got?

 

you can use 

Configuration object to store information about variables

 

if you want to store more informations for one instance of variable - i think that you will need to create separated table for it,

or do some workaround with Configuration object 

 

you can store several values of one variable in that way value1|value2|value3|value4

 

then you can do explode php function on that variable,

then foreach loop on it

 

you will be able to access to each value then

Link to comment
Share on other sites

Hello vekia,

 

Thanks for your quick reply.

 

I didn't understand what you are saying. Could you please show a piece of code that how can I access configuration object.

 

I will prefer check boxes over multiselect,

 

I have found multiselect in AdminPatternsController.php but didn't know how to store multiple values using multiselect

 

array(
                    'type' => 'select',
                    'label' => 'select multiple with chosen',
                    'name' => 'type_select_multiple_chosen',
                    'class' => 'chosen',
                    'multiple' => true,
                    'options' => array(
                        'query' => Country::getCountries((int)Context::getContext()->cookie->id_lang),
                        'id' => 'id_zone',
                        'name' => 'name'
                    ),

 

Please help me.

Link to comment
Share on other sites

I have implement multiselect and successfully saved the value with comma in the database field.

 

Now I have having a problem when I open a record in edit mode I want to show the previously selected values.

 

Can anyone please tell me how can I implement that.

 

Thanks in advance

Link to comment
Share on other sites

I have found the solution for this if anyone wants to implement mutiselect option in admin panel then he can add select box like

 

array(
                    'type' => 'select',
                    'label' => $this->l('Select Categories'),
                    'name' => 'categories[]',
                    'class' => 'chosen',
                    'multiple' => true,
                    'required' => true,
                    'options' => array(
                        'query' => $obj->getCategories(),
                        'id' => 'id_category',
                        'name' => 'name'
                    )
                ),

 

and show select values at the time of edit can be done by the following code

 

$this->fields_value = array(
        'categories[]' => array(5,6)
    );

 

 

It will show option of value 5 and 6 selected

Link to comment
Share on other sites

  • 5 months later...

I have found the solution for this if anyone wants to implement mutiselect option in admin panel then he can add select box like

 

and show select values at the time of edit can be done by the following code

 

It will show option of value 5 and 6 selected

I am using the helper form class . I have tried your suggested method for multi select. Its doing good for insert into the database but when I am trying for edit it is not getting the values as selected.

Can you tell me how to get those values in edit? 

Link to comment
Share on other sites

  • 9 months later...

Hi Chetanilhead, I have created multiselect dropdown in admin customer form,using this code

 

 array(
'type' => 'select',
'label' => $this->l('Department'),
'name' => 'department_or_branch[]',
//'id' => 'department_or_branch',
'required' => false,
'multiple' => true,
'options' => array(
'query' => $list_department_arr,
'id' => 'department_or_branch',
'name' => 'name',
'default' => array(
'value' => 0,
'label' => $this->l('All')
)
),
'col' => '4',
'hint' => array(
$this->l('All Department need to be listed here.')
)
),
 
It is showing errr "The department_or_branch field is invalid."
 
Please help me how to save the values to database.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...