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 );