Count of specific choices selected in a range of radio button fields

Hi,

I have a form which contains a list of radio button questions with the same 5 choices.

Can you please help me get a count of all the A’s, B’s, C’s, D’s and E’s.

I have tried using the gform_pre_submission approach that was provided in another thread but can’t seem to get the code right.

Here is what I have so far:

// change 258 to your form ID here
add_action( ‘gform_pre_submission_258’, function ( $form ) {
GFCommon::log_debug( METHOD . ‘(): The POST => ’ . print_r( $POST, true ) );
// What is the ID of the first radio button field that will be counted
$first_field = 55;
// What is the ID of the last radio button field that will be counted
$last_field = 65;
// initialize an empty array to hold all our radio button fields
$radio_fields = array();
// loop through all the radio button fields and assign them to an array
for ( $i = $first_field; $i <= $last_field; $i++ ) {
GFCommon::log_debug( METHOD . “(): Counter is {$i}.” );
$radio_fields[] = rgpost ( 'input
’ . $i );
}
$counts = array_count_values ( $radio_fields );
GFCommon::log_debug( METHOD . '(): COUNTS => ’ . print_r( $counts, true ) );
// store those counts in five fields in the form
$_POST[‘input_5295’] = $counts[‘NCR - Cat 1: An activity likely to cause death/ SBO / SBI’];
$_POST[‘input_5297’] = $counts[‘NCR - Cat 2 - An activity likely to cause other than death/ SBO / SBI’];
$_POST[‘input_5298’] = $counts[‘NCR - Cat 3 - Contravention of admin requirements’];
$_POST[‘input_5299’] = $counts[‘Compliant’];
$_POST[‘input_5300’] = $counts[‘Opportunity for Improvement’];
GFCommon::log_debug( METHOD . '(): MODIFIED POST => ’ . print_r( $_POST, true ) );
} );

I would really appreciate it if you took a look.

Thanks,
Lloyd

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.