Create & Update CSV on form submission [RESOLVED]

I am attempting to create and update a CSV file to store form submissions. Every time there is a new submission, I would like to append the entries to a new line. In this case, I am only interested in form 11.

I wrote a function(populate_csv) to open/create a CSV, create the headers, and populate form data from three fields (1,3,4).

My function created the csv and headers, but no data is being populated from the three fields I have specified. I

Here is a link to the code I have added to functions.php

Any advice is appreciated.

I dropped the foreach loop and tried using fputcsv on $data directly. Now, the header and $data print to the csv file, however Field ID#4 is not printing.

Is there an issue with the way I am trying to build an array when one of the objects is a checkbox?

Here is my updated code

Here is a screenshot of my form. Field are 1,4,3 respectively

What filter are you using? Can you add that to the updated pastebin please?

Hey @chrishajer ,

Sorry I didn’t include that in my second link. I’m using the same one as the first link. Here is what I currently have.

I’m getting very close here. I realized I made a mistake in using the wrong mode for ‘fopen’ I was using the ‘w’ parameter instead of ‘a’. Now each new entry is being appended. Unfortunately, the way I wrote this function, it is also appending the headers for each entry. I will work on a solution for this now.

I also figured out why I was not getting data from the checkbox field. I was only referencing the field ID and not the checkbox ID’s. I am going to work on re-writing my code to build ‘Reason’ into an array so that I do not have three columns and headers for one field.

Here is my latest code if anyone is interested

1 Like

For future reference, below is the code I ended up with. It looks to see if the CSV file exists - If not, the headers are created and related data appended. If the file exists, the form data is simply appended to a new row in the CSV file.

The reason behind creating this script is to feed real-time form data to our Qlik server. With this data, we can display a “prettified” version to almost any digital medium.

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

Good deal! Thank you for sharing.

1 Like