In my example I am collecting form data from a Gravity Forms radio button field (Field ID 4). Field ID 4 has a value of 0 for the selection of "First Choice". So to address the field I am using the variable $field_id and the selection value is gathered by using $entry[$field_id] (which is the value of 0) and the text label for this value is "First Choice". The expected result of field ID 4 with a value of 0 is the text "First Choice". I need this printed to a text file. I have done the following:
$field_id = 4;
$field = GFFormsModel::get_field( $form, $field_id );
$choice_text = $field['choices'][$entry[$field_id]]['text'];
$data = $choice_text;
file_put_contents($my_file,$data);
$tmpfile = fopen($my_file, "r");
$contents = fread($tmpfile, filesize($my_file));
The above approach will print "First Choice" to my text file as expected. However I need to do this with a function. If I try to do this with the following function I am returning 0 for some reason:
function choiceLabel($field_id){
$field = GFFormsModel::get_field( $form, $field_id );
$choice_text = $field['choices'][$entry[$field_id]]['text'];
return $choice_text;
}
$data = choiceLabel(4);
file_put_contents($my_file,$data);
$tmpfile = fopen($my_file, "r");
$contents = fread($tmpfile, filesize($my_file));
This produces an error that says "Warning: fread(): Length parameter must be greater than 0". Why is my function returning a 0 value instead of the string "First Choice"? Thanks for looking at this.
Thanks to David for pointing me in the direction of scope.
This works:
function choiceLabel($field_id,$form,$entry){
$field = GFFormsModel::get_field( $form, $field_id );
$choice_text = $field['choices'][$entry[$field_id]]['text'];
return $choice_text;
}
$data = choiceLabel(4,$form,$entry);