• Resolved fonefixer

    (@fonefixer)


    Hi Guys, i’m a relative novice at coding, so please bear with me. I’ve done hours of searching but can’t find an answer to this, unfortunately.

    I have modified the example code kindly given by the plugin author to meet my needs thus:

    // Drugs filter
    function DrugsFilter($formData){
        $formName = 'Prescription'; // change this to your form's name
        if ($formData && $formName == $formData->title) {
            // Create a consolidated "Drugs" field
            $formData->posted_data['Drugs'] =
                    $formData->posted_data['Drug1'] . ', ' .
                    $formData->posted_data['Drug2'] . ', ' .
    			    $formData->posted_data['Drug3'] . ', ' .
    			    $formData->posted_data['Drug4'] . ', ' .
    			    $formData->posted_data['Drug5'] . ', ' .
    			    $formData->posted_data['Drug6'] . ', ' .
    			    $formData->posted_data['Drug7'] . ', ' .
    			    $formData->posted_data['Drug8'] . ', ' .
    			    $formData->posted_data['Drug9'] . ', ' .
    			    $formData->posted_data['Drug10'] . ', ' .
    			    $formData->posted_data['Drug11'] . ', ' .
    			    $formData->posted_data['Drug12'] . ', ' .
    			    $formData->posted_data['Drug13'] . ', ' .
    			    $formData->posted_data['Drug14'] . ', ' .
    			    $formData->posted_data['Drug15'];
     
            // If you like, remove the original fields
            unset($formData->posted_data['Drug1']); 
            unset($formData->posted_data['Drug2']); 
            unset($formData->posted_data['Drug3']);
    		unset($formData->posted_data['Drug4']);
    		unset($formData->posted_data['Drug5']);
    		unset($formData->posted_data['Drug6']);
    		unset($formData->posted_data['Drug7']);
    		unset($formData->posted_data['Drug8']);
    		unset($formData->posted_data['Drug9']);
    		unset($formData->posted_data['Drug10']);
    		unset($formData->posted_data['Drug11']);
    		unset($formData->posted_data['Drug12']);
    		unset($formData->posted_data['Drug13']);
    		unset($formData->posted_data['Drug14']);
    		unset($formData->posted_data['Drug15']);
        }
        return $formData;
    }
     
    add_filter('cfdb_form_data', 'DrugsFilter');
    

    Basically I have 15 form fields which are amalgamated into 1 field in the database. My problem is the separators – all but the first drug field in the form are optional, to tidy things up i’d like to hide the separating comma and space if the relevant field is left blank by the user.

    Sorry if this is a simple thing i’ve overlooked but any help would be greatly appreciated, thanks.

    • This topic was modified 7 years, 10 months ago by fonefixer.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    Try this:

    
    // Drugs filter
    function DrugsFilter($formData) {
        $formName = 'Prescription'; // change this to your form's name
        if ($formData && $formName == $formData->title) {
            // Create a consolidated "Drugs" field
    
            $listOfDrugs = [];
            for ($idx = 1; $idx <= 15; ++$idx) {
                $fieldName = 'Drug' . $idx;
                if (isset($formData->posted_data[$fieldName]) &&
                        $formData->posted_data[$fieldName]
                ) {
                    $listOfDrugs[] = $formData->posted_data[$fieldName];
                    unset($formData->posted_data[$fieldName]);
                }
            }
            $formData->posted_data['Drugs'] = implode(', ', $listOfDrugs);
        }
        return $formData;
    }
    
    add_filter('cfdb_form_data', 'DrugsFilter');
    
    Thread Starter fonefixer

    (@fonefixer)

    Thanks very much, worked like a charm!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide separator if field is blank / no input.’ is closed to new replies.