Support is currently limited. Click here to see our hours of operation.
Help Desk

PHP & Repeatable Field Entries

Labels

This Ticket is public

Notifications

Is there a guide somewhere on how to use PHP to create a form entry that has multiple repeatable values?

For example...
Let's say I have a form to add families; and all the kids are in a repeatable region containing name, age & favorite color.

I need to do this programmatically because I have data on over 21,000 families; and need to add the children as a repeatable region.

HERE is the code I am attempting to use:

global $frm_form, $frm_field;
FrmEntry::create(array(
'form_id' => 21, //form id
'frm_user_id' => '1', //post as admin
'item_meta' => array(
410 => array( // repeating region id
412 => array('Rex','Alice'), // name field in repeating region
414 => array('Purple','Orange') // color field in repeating region
),
415 => 'Maurice' //field outside of repeating region
),
));

Thanks in advance.

I think it would look like this, but I haven't tested it:

410 => array( // repeating region id
  array( 412 => 'Rex', 414 => 'Purple' ),
  array( 412 => 'Alice', 414 => 'Orange' ),
),

Does that work for you?

actually... It has gotten quite involved. surely it should be easier than this...

///////////////////////////////// create main family entry & return the entry id
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
global $frm_form, $frm_field;
$myentry = FrmEntry::create(array(
'form_id' => 20,
'frm_user_id' => 1,
));

foreach($mynames as $kid){
//////////////////////// loop through kids & generate kids entries for each
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
$myentid = FrmEntry::create(array(
'frm_user_id' => 1,
'form_id' => 22,
'parent_item_id' => $myentry, //// entry of family
'item_meta' => array(
367 => $kid, ///kid name field
),
));
$myids .= $myentid.","; ////////assemble a comma sep string of kid entry ids
}

////////////////////////////////////////////////////////////////// assemble serialized kid ids
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
$myidcoll = explode(',',$myids);

/////////////////////////////////////////////////////////////////// update meta data via direct...
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
$servername = "localhost";
$username = "UN";
$password = "PW";
$dbname = "DB";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO wp_frm_item_metas (meta_value, field_id, item_id)VALUES('".serialize($myidcoll)."','365','".$myentry."')";
$conn->close();

whenever I tried to edit the meta with........$frm_entry_meta->update_entry_meta($entry_id, $field_id, $meta_key = null, $meta_value);.......it wouldn't work. Nor would it create the serialized array entry in the meta table if I specified the form id. This caused issues for the main entry in the items table since there wasn't a form id reference. So, I had to use a direct connection to the db instead.

Yes I figured it all out...but after working til 4am. Isn't there a better way?

Did you try my last suggestion instead of manually adding each repeating entry manually?

Yes- I had actually tried the scaled down version I initially posted with the name only; like this:

 

global $frm_form, $frm_field;
FrmEntry::create(array(
‘form_id’ => 21, //form id
‘frm_user_id’ => ‘1’, //post as admin
‘item_meta’ => array(
410 => array(     // repeating region id
412 => array(‘Rex’),     // name field in repeating region
),
415 => ‘Maurice’ //field outside of repeating region
),
));

but for some reason, I couldn't get it to create the entry until I split everything apart as shown above.

That's not quite the same as my last example. Can you try it?

 

No luck. Here's the full code:

<?php header("Access-Control-Allow-Origin: *"); ?>
<?php require( 'wp-load.php' );

global $frm_form, $frm_field;

echo FrmEntry::create(array(
‘form_id’ => 21, //form id
‘frm_user_id’ => ‘1’, //post as admin
‘item_meta’ => array(
410 => array( // repeating region id
array( 412 => 'Rex', 414 => 'Purple' ),
array( 412 => 'Alice', 414 => 'Orange' ),
),
415 => ‘Maurice’ //field outside of repeating region
),
));?>

This worked for me:

echo FrmEntry::create(array(
	'form_id' => 568, //form id
	'frm_user_id' => 1, //post as admin
	'item_meta' => array(
		12109 => 'field 1',
		12110 => array( // repeating region id
			'form' => 569, // the id of the repeating form
			array( 12112 => 'Rex' ),
			array( 12112 => 'Alice' ),
		),
	),
));

If you're not sure what to use in place of 'form' => 569, you can inspect the page source and find the hidden field at the beginning of the repeating section like this:
<input type="hidden" value="569" name="item_meta[12110][form]">

ok- thanks for your help.

Ticket closed.