0
votes

I have created a form using Gravity Forms on WordPress. I am now attempting to build and append new form entries to a CSV file so that I can feed the data to Qlik.

I am using the PHP action hook gform_after_submission to get the entry data from Gravity Forms. My form(#11) has 3 fields(1,4,3), however field ID #4 has three values

I am then passing that data through fputcsv.

I was finally able to create the CSV file, populate the headers, and populate the data. Unfortunately, the way I wrote my function, the headers were printing every time a new entry was created. I added an if statement to check if the file exists and only add headers if it does not. It did not work as expected, so I manually added the headers and now it works as expected. If anyone can help point me in the right direction to programatically add the headers, I would greatly appreciate it.

My bigger issue is with the data field 'Reason'. It is only one field, but a checkbox with multiple values. Because of this, I cannot directly reference the ID(4) of the field, but instead have called each value independently. I tried building an array with all three values, but was unable to figure it out.

If you've read this far, I appreciate you taking time out of your day. Any advice/input is appreciated.

Here is my current function and action hook:

function populate_csv( $entry, $form ) {

//Headers info
$headers = array('Nominee', 'Reason1', 'Reason2', 'Reason3', 'Justification');

//Build form data
$data = array(
    'Nominee' => rgar( $entry, '1' ),
    'Reason1' => rgar( $entry, '4.1' ),
    'Reason2' => rgar( $entry, '4.2' ),
    'Reason3' => rgar( $entry, '4.3' ),
    'Justification' => rgar( $entry, '3' ), 
             );

//Open or Create CSV File
$fh = fopen('Nominations.csv', 'a');

// Check to prevent overwriting the headers 
if (!file_exists('Nominations.csv')) {
    //Create headers
    fputcsv($fh, $headers);
}
//Populate the data 
fputcsv($fh,$data);

//Close the file
fclose($fh);
}
add_action( 'gform_after_submission_11', 'populate_csv', 10, 2 );
1

1 Answers

0
votes

The issues is that you are checking if the file exists after you create the file. Check if it exists first, then open/create it. See below:

function populate_csv( $entry, $form ) {

//Headers info
$headers = array('Nominee', 'Reason1', 'Reason2', 'Reason3', 'Justification');

//Build form data
$data = array(
    'Nominee' => rgar( $entry, '1' ),
    'Reason1' => rgar( $entry, '4.1' ),
    'Reason2' => rgar( $entry, '4.2' ),
    'Reason3' => rgar( $entry, '4.3' ),
    'Justification' => rgar( $entry, '3' ), 
             );

// check if the file exists or not to determine if headers are needed
$headersNeeded = !file_exists('Nominations.csv');

//Open or Create CSV File
$fh = fopen('Nominations.csv', 'a');

// if headers are needed, add them  
if ($headersNeeded) {
    //Create headers
    fputcsv($fh, $headers);
}
//Populate the data 
fputcsv($fh,$data);

//Close the file
fclose($fh);
}
add_action( 'gform_after_submission_11', 'populate_csv', 10, 2 );