Consolidating Dynamic Population Scripts

I’d like to consolidate my dynamic population scripts into one.I have several forms looking for the same information, and would like to remove the _{id} from the end of the filters:
add_filter( ‘gform_pre_render’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_pre_validation’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_admin_pre_render’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_pre_submission_filter’, ‘c2c_get_dynamic_info’ );

I tried this:
global $wpdb;
$choices = array();
switch($form->id):

 case 6 :
        foreach ( $form['fields'] as &$field ):     

            if( $field->id == 1 ) : 
                $lines = c2c_get_lines($field);

endif;
endforeach;
break;
endswitch;

But it doesn’t work. It works fine if the switch routine is removed.

Anyone? Please and thank you…

Can you please post all your code with no comments? You can use three backticks on a blank line, then your code, then another three backticks on a blank line to preserve code formatting. It will look like this in the form editor (this is an image):

chrome_2018-11-19_11-41-07

And render like this:

add_filter( ‘gform_pre_render’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_pre_validation’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_admin_pre_render’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_pre_submission_filter’, ‘c2c_get_dynamic_info’ );

Use the preview on the right to see if it worked correctly.

add_filter( ‘gform_pre_render’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_pre_validation’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_admin_pre_render’, ‘c2c_get_dynamic_info’ );
add_filter( ‘gform_pre_submission_filter’, ‘c2c_get_dynamic_info’ );
function c2c_get_dynamic_info($form) {
    global $wpdb;
    switch($form->id) :
        case 1:
            foreach ( $form['fields'] as &$field ) :
                if( $field->id == 17 ) $line = $_GET['line'];
            endforeach;;
        break;
    endswitch;
}
function c2c_get_lines($field){
    global $wpdb;
    $choices = array();
    $choices[] = array('text' => '','value' =>'');
    $sql = "
        SELECT id, line_name 
        FROM " . CRZ . "lines 
        ORDER BY line_name";
    $lines = $crzdb->get_results($sql);
    foreach ($lines as $_) {
        $choices[] = array('text' => $_->line_name, 'value' => $_->id);
    }
    $field->choices = $choices;
}

Thank you

Thank you for the code. What happens now when you use this?

I recommend adding some logging statements to your code to see what each variable holds when you assign it.