setQuery( "SELECT * FROM #__comprofiler_fields WHERE `table`='#__comprofiler' AND name 'NA' AND registration = '1'" ); $fields = $database->loadObjectList(); $fields2 = array('id', 'user_id'); $fields3 = array(); foreach($fields as $field){ $fields2[] = $field->name; $fieldname = $field->name; $fields3[] = JRequest::getVar($params->get($fieldname), '', 'post', 'string');//mosGetParam($_POST, $params->get('$fieldname'), ''); } $database->setQuery( "INSERT INTO #__comprofiler (".implode(",",$fields2).") VALUES ('".$user->get('id')."','".$user->get('id')."','".implode("','",$fields3)."');" ); if (!$database->query()) { JError::raiseWarning(100, $database->getErrorMsg()); }And I am guessing I must do it here somewhere. I am php-"capable" but not very good. I suppose we should test each value inside the $fields3-array for also being an array and then implode it, but how to do this?Bert"> [SOLVED] CB Plugin: checkbox array - Forums

Forums

[SOLVED] CB Plugin: checkbox array

LPent 09 Jun, 2009
Well I am making a mess of things today. After posting a few stupid questions today I deleted some of them and took a nap. Fully refreshed I managed to get this plugin working almost perfectly. There is one thing still not working though.

If I have an array of multiple checkboxes they are not properly imported into the CB database. It actually says "Array" in the dbase. So I am guessing I must implode these values first.

Anyway, I found this code in the plugin:


/********************CB part*************************/
		$database->setQuery( "SELECT * FROM #__comprofiler_fields WHERE `table`='#__comprofiler' AND name <>'NA' AND registration = '1'" );
		$fields = $database->loadObjectList();
		$fields2 = array('id', 'user_id');
		$fields3 = array();
		foreach($fields as $field){
			$fields2[] = $field->name;
			$fieldname = $field->name;
			$fields3[] = JRequest::getVar($params->get($fieldname), '', 'post', 'string');//mosGetParam($_POST, $params->get('$fieldname'), '');
		}
		$database->setQuery( "INSERT INTO #__comprofiler (".implode(",",$fields2).") VALUES  ('".$user->get('id')."','".$user->get('id')."','".implode("','",$fields3)."');" );
		if (!$database->query()) {
			JError::raiseWarning(100, $database->getErrorMsg());
		}


And I am guessing I must do it here somewhere. I am php-"capable" but not very good. I suppose we should test each value inside the $fields3-array for also being an array and then implode it, but how to do this?

Bert
GreyHead 10 Jun, 2009
Hi LPent,

There's a box on the General Tab (used to be on the Autogenerated tab) called "ChronoForms handle my posted arrays:" Is this set to Yes?

If not please try it.

Bob
LPent 10 Jun, 2009
Hi Bob,

The setting was allready set to "yes". I tried it in the "no" position none the same, but no dice.
By the way, I am using ChronoForms 3.1 RC5.0 and the setting is still on the "autogenerated" tab and not on the "general" tab?

Any other ideas?

Thanks,
Bert
LPent 11 Jun, 2009
Hi,

I am trying to get a grip on this with my limited knowledge of php, What I have so far is the following (code comes from cf_cb_registration.php, and my alterations are between the //** mycode comments):


/********************CB part*************************/
		$database->setQuery( "SELECT * FROM #__comprofiler_fields WHERE `table`='#__comprofiler' AND name <>'NA' AND registration = '1'" );
		$fields = $database->loadObjectList();
		$fields2 = array('id', 'user_id');
		$fields3 = array();
		foreach($fields as $field){
			$fields2[] = $field->name;
			$fieldname = $field->name;
			$fields3[] = JRequest::getVar($params->get($fieldname), '', 'post', 'string');//mosGetParam($_POST, $params->get('$fieldname'), '');
		}
		//** mycode
		$yuva_count = count($fields3);
		for ($yuva_t = 0; $yuva_t < $yuva_count; $yuva_t++) {
			if (is_array($fields3[$yuva_t])) {
				$fields3[$yuva_t] = implode('|*|', $fields3[$yuva_t]);
				}
		}
		echo(implode($fields3));
		//** end mycode
		$database->setQuery( "INSERT INTO #__comprofiler (".implode(",",$fields2).") VALUES  ('".$user->get('id')."','".$user->get('id')."','".implode("','",$fields3)."');" );
		if (!$database->query()) {
			JError::raiseWarning(100, $database->getErrorMsg());
		}
		/**********************************************/


I am using the |*| as delimiter because that seems to be the way CB stores it's arrays in mysql.

Doing this still gives me the word "Array" however instead of the values. Could someone please check my code and tell me what I am doing wrong?

Thanks,
Bert
LPent 11 Jun, 2009
Ok I found it!!!

I'll post here for future reference for others and maybe so it can be implemented in the next version?

1) The checkbox values need to be EXACTLY the same in Chronoforms and in CB!! If not, the values will be stored in CB but they won't show up as checked values.

2) In order to explode checkboxes, you can actually use the function "handleArrays" in "chronoform.php" but you will need to change that function slightly.

In chronoform.php:

function handleArrays($formname, $MyDelim=', '){
		global $mainframe;
		$posted = JRequest::get( 'post' , JREQUEST_ALLOWRAW );
		$MyForm =& CFChronoForm::getInstance($formname);
		/**
		 * Associate field values with names and implode arrays
		 */
		$fields = array();
		$names = explode(",", str_replace("[]", "" , $MyForm->formrow->fieldsnames));
		foreach ( $names as $name ) {
			if($MyForm->formparams('handlepostedarrays') == 'Yes'){
				if(isset($posted[$name])){
					if ( is_array($posted[$name])) {
						$fields[$name] = implode($MyDelim, $posted[$name]);
						JRequest::setVar($name, implode($MyDelim,$posted[$name]));
					} else {
<snip>

What I did here was add the $MyDelim attribute.

Now we can do this in cf_cb_registration.php:

/********************CB part*************************/
		$MyForm->handleArrays($MyForm->formrow->name, '|*|');


3) another thing I did was add the following to cf_cb_registration.php:


if ( !$user->save() )
		{
			$MyPlugins->cf_cb_registration['errors'] = JText::_( $user->getError());
			$MyForm->addErrorMsg(trim('Some error occured')); <<<<< I added this line
			return false;
		}


This line reloads the form, shows an error and prevents from sending an e-mail to the user when registration failed for some reason (most likely because the username allready exists).

Hope this helps others.
mikec 20 Aug, 2009
Please be aware that the first line of the function needs to look like this
function handleArrays($formname, $MyDelim='|*|')
not this
function handleArrays($formname, $MyDelim=',')
or you will just get the same "," separator in the DB.

Duh!!!🙄
LPent 20 Aug, 2009
Actually, No.

The $MyDelim=', ' just makes sure we don't break the original code. This function is used by chronoengine somewhere (can't remember where) and there it needs a ', '. So by using this it defaults to a comma (so it won't break chronoengine) but when calling the function with $MyDelim='|*|' we override this default, which is what we need for CB.
mikec 24 Aug, 2009
Next problem we are having. After solving the problems with checkbox value's being written to the db in the proper format. |*| is now working until we turn on the confirmation plug-in then we go back to "," separators. Please advise on how to use the confirmation page plug-in and retain the |*| delimiter.
GreyHead 25 Aug, 2009
Hi MikeC,

You need to make the same change in the Plugin - I emailed you the line number.

Bob
mikec 25 Aug, 2009
This did the trick. Just to clarify you need to put the |*| on line 319 of the cf_confirmation page for this to write back to CB with proper delimiter. See below.

change
$post = implode(", ", $post);


to

$post = implode("|*| ", $post);




Thanks Greyhead!
acy277 26 Aug, 2009
Hi guys,

I got this problem as well and have followed Lpent's method but no luck at the beginning.

I have taken many hours working on it and finally found the solution.

First I have changed
function handleArrays($formname, $MyDelim=', ')

to
function handleArrays($formname, $MyDelim='|*|')


in chronoform.php, then I have added "[]" at the end of the checkbox's name (e.g. name="interests[]"), in the cb plugin setting, keep "interests" as the same.

This works fine for me. Thanks for Lpent's modified code.
LPent 26 Aug, 2009
Glad to see my research is helping others.

I would like to point out though that changing the default of the MyDelim argument is NOT recommended (it will break chronoengine when you use other functions/plugins).

Perhaps the reason for it not to work for some is because at the end of the handleArray function there are are also changes I made which maybe are not so obvious? I recommend you copy/paste the entire code I posted above or at least walk through it line by line.

Also, this is a "dirty" solution. Hopefully this functionality will be included in some future version of the CB plugin?
daemonhunt 30 Aug, 2009
I am trying this code with a multi-select drop down, and no joy :-(

I have no idea what I am doing wrong. It's perplexing!

Anyone have any guidance when trying to pass CF multi-select to CB multi-select? My values are EXACTLY the same in both. Do I put the selct id into the corresponding box in the CF_CB_Plugin?
LPent 30 Aug, 2009
daemonhunt: I have no experience with frop down boxes and I have no means of testing it for you at the moment. But I would start by:

1) checking how the values are stored in the CB table in MySQL (via phpmyadmin).
2) checking how the values are output by CF (simply echo out the variable or array in the code)
daemonhunt 08 Sep, 2009
Okay I still can't figure the drop-downs out, so I am reverting to a checkbox array. Is this form code below correct for my form? What value to I put into the relative field in the CB Plugin?

<input class="radio" type="checkbox" value="Sprints" title="Sprints" name="check0[]" id="event_group_0" />
    <label for="event_group_0" class="check_label">Sprints</label>

    <input class="radio" type="checkbox" value="Hurdles" title="Hurdles" name="check1[]" id="event_group_01" />
    <label for="event_group_01" class="check_label">Hurdles</label>
    
    <input class="radio" type="checkbox" value="Distance" title="Distance" name="check2[]" id="event_group_02" />
    <label for="event_group_02" class="check_label">Distance</label>
    
    <input class="radio" type="checkbox" value="Throws" title="Throws" name="check3[]" id="event_group_03" />
    <label for="event_group_03" class="check_label">Throws</label>
    
    <input class="radio" type="checkbox" value="Jumps" title="Jumps" name="check4[]" id="event_group_04" />
    <label for="event_group_04" class="check_label">Jumps</label>
    
    <input class="radio" type="checkbox" value="Race Walking" title="Race Walking" name="check5[]" id="event_group_05" />
    <label for="event_group_05" class="check_label">Race Walking</label>
    
    <input class="radio" type="checkbox" value="Multi Events" title="Multi Events" name="check6[]" id="event_group_06" />
    <label for="event_group_06" class="check_label">Multi Events</label>
    
    <input class="radio" type="checkbox" value="Young Athletes" title="Young Athletes" name="check7[]" id="event_group_07" />
    <label for="event_group_07" class="check_label">Young Athletes</label>
    
    <input class="radio" type="checkbox" value="AwD" title="AwD" name="check8[]" id="event_group_08" />
    <label for="event_group_08" class="check_label">AwD</label>
GreyHead 08 Sep, 2009
Hi daemonhunt,

If you want them all to be in the *same* array then they should all have the *same* array name e.g. name="event[]"

Bob
daemonhunt 08 Sep, 2009
Thanks Bob, One answer at a time eh? And my other question? Do I put this array name in the CB Registration Plugin (to match up with the CB multiselect field)? Or do I need to have the *same* ID value as well and put that in there?
GreyHead 08 Sep, 2009
Hi daemonhunt,

It's only 6am here - a bit early to read more than one post at a time.

Apart from the info others have posted in this thread I have no idea about saving info in CB. I would expect that the field names need to match up e.g. 'event' or 'check' in both places.

Bob
daemonhunt 30 Sep, 2009
This still isn't working for me. I need some advice that is more solid. Anyone?
LPent 30 Sep, 2009
Well, we are working half blind here as we can't really see what you are trying to do.

So far all I can tell you is that IDs have nothing to do with it. The VALUEs in the form must match up with the values you assigned in CB (these must be the same). Next you assign the NAME of the form elements (plural) to the NAME of the CB element (singular) (these can be different if you want).
The problem may be because you gave all the checkboxes a different name? A "set" of checkboxes all need the same name (otherwise how can the computer tell they are all part of the same set?)
daemonhunt 14 Oct, 2009
Okay I have got somewhere. My checkbox fields look like:

<input class="radio" type="checkbox" value="Sprints" name="event_group" />
<label class="check_label">Sprints</label>

<input class="radio" type="checkbox" value="Hurdles" name="event_group" />
<label class="check_label">Hurdles</label>


And yes, the values between these CF fields and my CB custom fields match, and it is actually working!

The problem now is that if I check both of these checkboxes, only the last one I checked (e.g. hurdles) is being passed on to CB. I have no idea why not both.

I have tried setting name="event_group[]" instead for these but this just and up saying "Array" in CB.

I have tried using LPent's code from earlier in this post, and I have tried without, but get exactly the same results as with and without [].

Any clue as to what I should do? I'm so close now!
daemonhunt 14 Oct, 2009
Okay, I have confirmed by analysing the debug. These fields are incorrect:

<input class="radio" type="checkbox" value="Sprints" name="event_group" />
<label class="check_label">Sprints</label>

<input class="radio" type="checkbox" value="Hurdles" name="event_group" />
<label class="check_label">Hurdles</label>


The input names must have [] after their name, and in the CF_CB_Plugin, one must just use the input name without the [] to indicate the corresponding inputs.

But, now I am back to square one. Without any of the hacks at the beginning of this post, or indeed With the hacks, the only value that gets returned in CB is just the word "Array" and nothing is checked at all when I go to edit the profile.

Somehow this array is not being transferred properly to CB.

Here is what the CF debug says:

[event_group] => Array ( [0] => Sprints [1] => Hurdles )


I suspect that the word "Array" is coming from this, and somehow I need it to be exploded.

How can I do this?
GreyHead 15 Oct, 2009
Hi daemonhunt,

Then I suspect that you haen't got ChronoForms set up to handle arrays for you. There's a setting on the Form General Tab.

Bob
daemonhunt 15 Oct, 2009
Hi Bob of course this is on. It's on in the master form and on all the child forms.

I've tried it on and I've tried it off. Bob, I have tried many different combinations, and still no luck.

Incidentally it comes out properly in the email with commas between each item... it just seems to be when it is sent to Community Builder (latest version) that it doesn't work.

Any more suspicions?
GreyHead 15 Oct, 2009
Hi daemonhunt,

Sorry, no useful suggestions, I know little about the old version of CB and less about the new one. I do understand that arrays need to imploded with the |*| notation though.

Bob
This topic is locked and no more replies can be posted.