Forums

CheckBoxes Group save db in multiple columns

ciovo 20 Dec, 2011
Hello!

I'm quite new to ChronoForms, and I'd like to understand if it's possibile to save the content of a CheckBoxes Group in multiple columns in the database table (one choice/check -> one column) instead of just one column.
It's easier for me to make sql analysis having many columns than having all data in one comma-separated-text column.

How can I do this?

Thank you for your help!
GreyHead 20 Dec, 2011
Hi clovo,

Yes you can. There are two ways I can think of.

a) Give each of the checkboxes a different name - effectively using a lot of single checkboxes rather that a checkbox group. This is going to be messy if you have more than a handful of checkboxes.

b) Add custom code in the On Submit event to read the checkbox results and write them to separately names variables to save in the table columns.

Bob
ciovo 20 Dec, 2011

b) Add custom code in the On Submit event to read the checkbox results and write them to separately names variables to save in the table columns.



Thanks, that's what I was looking for, I think...
Can you help me with a basic code example to use as a guideline in the custom code?

Thank you!
GreyHead 20 Dec, 2011
Hi ciovo,

What is the Source code for your check box group?

Bob
ciovo 20 Dec, 2011

What is the Source code for your check box group?



<div class="ccms_form_element cfdiv_checkboxgroup label_over radios_over" id="select_container_div"><label>Select one or more region</label><input type="hidden" name="region" value="no_region" alt="ghost" />
<input type="checkbox" name="region[]" id="region_one" title="" value="one" class="validate['group[4]']">
<label for="region_one">One</label>
<input type="checkbox" name="region[]" id="region_two" title="" value="two" class="validate['group[4]']">
<label for="region_two">Two</label>
<input type="checkbox" name="region[]" id="region_three" title="" value="three" class="validate['group[4]']">
<label for="region_three">Three</label>
<div class="clear"></div><div id="error-message-region"></div></div>
GreyHead 21 Dec, 2011
Hi ciovo,

Please try this in a Custom Code action, it should be before any Handle Arrays action.
<?php
$regions = JRequest::getVar('region', array(), 'post', 'array');
$region_values = array('one' => 'one', 'two' => 'two', 'three' => 'three');
foreach ( $region_values as $k => $v ) {
  if ( in_array( $k, $regions ) ) {
    $form->data['region_'.$k] = $v;
  }
}
?>

This will put the values from the $region_values array into three form variables: region_one, region_two and region_three.

NB Not tested and may need debugging!

Bob
wydo 17 Apr, 2012
Hi Bob,

I found this thread which is exactly what I'm trying to do. I have a checkbox group with 9 options. I'd like to store them in separate columns in the table. So I tried to adapt the code you wrote there for ciovo. But it is not working.... I miss something. I activated the db store, generated a table for my form, and added 9 fields in that table. I added the custom code on submit. But it is not working, I get the following error :

Parse error: syntax error, unexpected '{' in ...../components/com_chronoforms/form_actions/custom_code/custom_code.php(18) : eval()'d code on line 5



By the way, I don't understand how this works, where is done the link between the variable name from the custom code and the store in the table field ? Is it "automatic" ?

I can give you the url to that form if you like...

Thanks a lot in advance for your help

wydo

Here is my checkbox group code :
<div class="ccms_form_element cfdiv_checkboxgroup radios_over" id="autoID-3b344e3d29229a897ae9c67d2b4615e5_container_div"><label>Discipline / Category</label><input type="hidden" name="input_discipline_categ" value="" alt="ghost" />
<div style="float:left; clear:none;"><input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_0" title="" value="RH-E-T (Disaster search qualif)" class="validate['group[6]']" />
<label for="input_discipline_categ_0">RH-E-T (Disaster search qualif)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_1" title="" value="RH-E-FL (Area search qualif)" class="validate['group[6]']" />
<label for="input_discipline_categ_1">RH-E-FL (Area search qualif)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_2" title="" value="RH-E-F (Tracking qualif)" class="validate['group[6]']" />
<label for="input_discipline_categ_2">RH-E-F (Tracking qualif)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_3" title="" value="RH-E-W (Water search qualif)" class="validate['group[6]']" />
<label for="input_discipline_categ_3">RH-E-W (Water search qualif)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_4" title="" value="RH-T A (Disaster search cat A)" class="validate['group[6]']" />
<label for="input_discipline_categ_4">RH-T A (Disaster search cat A)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_5" title="" value="RH-FL A (Area search cat A)" class="validate['group[6]']" />
<label for="input_discipline_categ_5">RH-FL A (Area search cat A)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_6" title="" value="RH-F A (Tracking cat A)" class="validate['group[6]']" />
<label for="input_discipline_categ_6">RH-F A (Tracking cat A)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_7" title="" value="RH-W A (Water search cat A)" class="validate['group[6]']" />
<label for="input_discipline_categ_7">RH-W A (Water search cat A)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_8" title="" value="RH-T B (Disaster search cat B)" class="validate['group[6]']" />
<label for="input_discipline_categ_8">RH-T B (Disaster search cat B)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_9" title="" value="RH-FL B (Area search cat B)" class="validate['group[6]']" />
<label for="input_discipline_categ_9">RH-FL B (Area search cat B)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_10" title="" value="RH-F B (Tracking cat B)" class="validate['group[6]']" />
<label for="input_discipline_categ_10">RH-F B (Tracking cat B)</label>
<input type="checkbox" name="input_discipline_categ[]" id="input_discipline_categ_11" title="" value="RH-W B (Water search cat B)" class="validate['group[6]']" />
<label for="input_discipline_categ_11">RH-W B (Water search cat B)</label>
</div><div class="clear"></div><div id="error-message-input_discipline_categ"></div></div>


Here is the cutom code, on submit, before handle arrays :
<?php
$categ = JRequest::getVar('input_discipline_categ', array(), 'post', 'array');
$categ_values = array('1' => '1', '2' => '2', '3' => '3','4' => '4','5' => '5','6' => '6','7' => '7','8' => '8','9' => '9',);
foreach ( $categ_values as $k => $v ) {
  if ( in_array($categ, $k ) {
    $form->data['input_discipline_categ_'.$k] = $v;
  }
}
?>
GreyHead 17 Apr, 2012
Hi wydo,

There's a ')' missing near the end of line 5
<?php
$categ = JRequest::getVar('input_discipline_categ', array(), 'post', 'array');
$categ_values = array('1' => '1', '2' => '2', '3' => '3','4' => '4','5' => '5','6' => '6','7' => '7','8' => '8','9' => '9');
foreach ( $categ_values as $k => $v ) {
  if ( in_array( $k, $categ ) ) {
    $form->data['input_discipline_categ_'.$k] = $v;
  }
}
?>
I'll fix my original post

Bob
wydo 17 Apr, 2012
Thanks a lot Bob,

Sorry, I know nothing about php :?

now I get a warning 9 times (as I have 9 fields I guess) :

Warning: in_array() [function.in-array]: Wrong datatype for second argument in ....../administrator/components/com_chronoforms/form_actions/custom_code/custom_code.php(18) : eval()'d code on line 5

and still no values in my fields in the table (I created them at the end of the field list, and they are named input_discipline_categ_1, input_discipline_categ_2 etc..)

Thank you!
wydo 17 Apr, 2012
Hi again,

I found the problem :

the parameters for in_array function are switched. It should be first the value searched ($k) then the array $categ

I is working, I'm really happy! thank you!
GreyHead 17 Apr, 2012
Hi wydo,

Well found :-( and apologies for my bug :-(

Bob

PS I'll correct my earlier post for other readers.
Terejima 07 Mar, 2013
Hi.
I try to do the same thing for my checkbox group but I don't understand the php code.

Here is my html code :

<div style="float:left; clear:none;"><input type="checkbox" name="langages[]" id="langages_0" title="" value="langages_0" class="" />
<label for="langages_0">PHP</label>
<input type="checkbox" name="langages[]" id="langages_1" title="" value="langages_1" class="" />
<label for="langages_1">Java</label>
<input type="checkbox" name="langages[]" id="langages_2" title="" value="langages_2" class="" />
<label for="langages_2">Langage C</label>


And my custom code, on submit, before handle arrays :

<?php
$langages = JRequest::getVar('langages', array(), 'post', 'array');
$langages_values = array('0' => '0', '1' => '1', '2' => '2');
foreach ( $langages_values as $k => $v ) {
  if ( in_array( $k, $langages ) ) {
    $form->data['langages_'.$k] = $v;
  }
}
?>


I had some line in my table but it only fill the "langages" line with comma-separated-text.


Can someone help me ?
Thanks in advance.
GreyHead 09 Mar, 2013
Hi Terejima ,

The values you have in the HTML are like value="langages_0" but in the code you are checking for the equivalent of value='0'

You need to change either the HTML to value='0', etc. or the array to
$langages_values = array('langages_0' => '0', 'langages_1' => '1', 'langages_2' => '2');


Either change is OK but they must both use the same set of values.

Bob
01 07 Oct, 2013
Hi, Bob!
Having difficulties. Data from check group isn't divided.
Here's my checkbox group code, the Custom Code and event tab scrennshot:
<div class="ccms_form_element cfdiv_checkboxgroup radios_over" id="input_checkbox_group_141_container_div" style=""><input type="hidden" name="input_checkbox_group_14" value="" alt="ghost" />
<div><input type="checkbox" name="input_checkbox_group_14[]" id="input_checkbox_group_14_0" title="Must select" value="общение между мужчиной + женщиной" class="validate['group[14]']" />
<label for="input_checkbox_group_14_0">„общение между мужчиной + женщиной“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" id="input_checkbox_group_14_1" title="Must select" value="отношения родители и дети" class="validate['group[14]']" />
<label for="input_checkbox_group_14_1">„отношения родители и дети“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" checked="checked" id="input_checkbox_group_14_2" title="Must select" value="коммуникативные ситуации на работе" class="validate['group[14]']" />
<label for="input_checkbox_group_14_2">„коммуникативные ситуации на работе“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" checked="checked" id="input_checkbox_group_14_3" title="Must select" value="разные бытовые ситуации общения" class="validate['group[14]']" />
<label for="input_checkbox_group_14_3">„разные бытовые ситуации общения“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" checked="checked" id="input_checkbox_group_14_4" title="Must select" value="избавление от комплексов и страхов" class="validate['group[14]']" />
<label for="input_checkbox_group_14_4">„избавление от комплексов и страхов“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" id="input_checkbox_group_14_5" title="Must select" value="адаптация в чужой стране" class="validate['group[14]']" />
<label for="input_checkbox_group_14_5">„адаптация в чужой стране“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" id="input_checkbox_group_14_6" title="Must select" value="общение с представителем другой культуры" class="validate['group[14]']" />
<label for="input_checkbox_group_14_6">„общение с представителем другой культуры“</label>
</div><div><input type="checkbox" name="input_checkbox_group_14[]" id="input_checkbox_group_14_7" title="Must select" value="other" class="validate['group[14]']" />
<label for="input_checkbox_group_14_7">other</label>
</div></div>

Custom code:

<?php
$check_group = JRequest::getVar('input_checkbox_group_14', array(), 'post', 'array');
$check_group_values = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7');
foreach ( $check_group_values as $k => $v ) {
  if ( in_array( $k, $check_group ) ) {

$a = check_group_.$k = $v;
print_r($a);
    $form->data['check_group_'.$k] = $v;
  }
}
?>

The new array is empty😟 and the data is still separated by comma
[attachment=1]first.png[/attachment]
[attachment=0]second.png[/attachment]
Thank you!
GreyHead 07 Oct, 2013
Hi 01,

What are you actually trying to do?

The values of your checkboxes are strings like 'отношения родители и дети' so they will never match the digits (0, 1, 2 . . .) you are comparing them to.

Bob
01 07 Oct, 2013
Hi Bob!
Now all values are written to DB in a one row, and values from checkbox are written with comma separator. For better statistics I need to write each selected value from checkbox group separately (in another row or probable DB(but in would be harder to implement))
GreyHead 08 Oct, 2013
Hi 01,

OK that's what the code does, but you need to match it to your set of options. The code was written for a set of options that look like:
1=„отношения родители и дети“
2=„общение между мужчиной + женщиной“
. . .
your options look like:
отношения родители и дети=„отношения родители и дети“
общение между мужчиной + женщиной=„общение между мужчиной + женщиной“
. . .

You need either to change the keys (the part before the =) in your options to make them simpler, or you have to modify the code to work with your string keys.

Bob
01 08 Oct, 2013
Hi, Bob!
Thank you for explanations!
I will try to get it working🙂
This topic is locked and no more replies can be posted.