Get the value for a field within an entry where parent field equals X.

By: Bobby Clapp | Asked: 09/23/2022
ForumsCategory: Code SnippetsGet the value for a field within an entry where parent field equals X.
Bobby Clapp asked 2 years ago

//Get Entry ID of a field and then return another fields value based on that ID (Within the same form)
function getLinkedFormData( $atts ) {
global $wpdb;
$field_id = $atts['field_id']; //Field ID used to capture the entry_id of the original entry
$where_value = $atts['where_value']; //Must be unique. A 'primary key' value that is used to link forms by a certain field value
$associated_field_id = $atts['associated_field_id']; //Field ID of a field in the original entry that is not the same as $field_id above
$getEntryID = $wpdb->get_var( $wpdb->prepare("SELECT item_id FROM ". $wpdb->prefix ."frm_item_metas WHERE field_id = %d AND meta_value = %s", $field_id, $where_value) );
return FrmProEntriesController::get_field_value_shortcode(array('field_id' => $associated_field_id, 'entry' => $getEntryID));
}
add_shortcode( 'get_frmlinked_data', 'getLinkedFormData' );

Example: [get_frmlinked_data field_id=256 where_value="Unique Entry Text Here" associated_field_id=258]

This looks for the Entry ID for the entry that contains the value "Unique Entry Text Here" and then returns any field value from within that same Entry ID as defined by the "associated_field_id". This is sort of an alternative to frm_field_value where you might need to have a where query and also certain types of views where you're only trying to capture 1 particular field value in an entry.

The only caveat here is that this will only return a single value referenced from an existing unique value. It does not return an array of values or have a way to determine a specific value from a array of values.

Walter JonesWalter Jones replied 2 years ago

Ah very useful!

2 Answers
Carlo Santos answered 2 years ago
Hi Bobby, I'm just trying to apply this code to my form but my limited coding knowledge is not helping. If I had to get the Entry ID of FormA then I would apply this: [get_frmlinked_data field_id=256 where_value="Unique Entry Text Here" associated_field_id=258] to a field in FormB? If Field ID 256 the same field that I add the above shortcode to(In FormB) Is Field ID 258 also from FormB? Regards
Carlo  
Bobby Clapp replied 2 years ago

Sorry I missed this. The code is limited, but this is the just of it: The "where_value" is a specific value that you will set that is a unique value to field 256. Example: I know that 256 has a unique value of "finance". Then it will find the value of field 258 in that same entry and display that back. Example: If within an entry field id 258 is 3000, where that entry for for 256 equals "finance", then that is the result this code would return the value of 3000.

Scuts Sgr answered 2 years ago
Hi -- thank you for posting this. I think that I may be misunderstanding something about the use.  Does the form read from only its entries? Can the value read from a field ID itself? So — Let’s say that there are 3 entries with corresponding info: Number A - Number B 1 - 86 2 - 87 3 - 88 A form has 3 field IDs Field ID: 18 ____ Field ID: 19 ____ Field ID: 20 ____ If a user types “2” into [18], I want 87 (“Number B”) to show in [20]. If type “1”, then “86” should show. I tried a couple of different shortcodes with that, but had no luck. I think that I may be misunderstanding something.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right