Skip to content

Instantly share code, notes, and snippets.

@leepettijohn
Created April 25, 2016 20:30
Show Gist options
  • Select an option

  • Save leepettijohn/bcf5c5c19129025916f2ae03bdcc726a to your computer and use it in GitHub Desktop.

Select an option

Save leepettijohn/bcf5c5c19129025916f2ae03bdcc726a to your computer and use it in GitHub Desktop.
Extract a CSV in Gravity Forms and put in a textarea
// Change the '9' at the end to the id of your form
add_action('gform_post_submission_9','pullcsv',10,2);
function pullcsv($entry, $form){
//Fields to change
$file_field = 3;
$text_field = 4;
$entry_id = $entry['id'];
$csv = $entry[$file_field]; //this returns a url string to the uploaded csv
$row = 1;
if (!empty($csv)){
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// $num refers to the number of fields in the line
// $row is the counter for each line
$num = count($data);
$confirmation .= "<p>".$num." fields in line ".$row.": <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
// $data[$c] refers to the entry in that line at that space
$confirmation .= $data[$c] . "<br />\n";
}
}
fclose($handle);
}
}
else {$confirmation = 'No CSV';}
GFAPI::update_entry_field($entry_id,$text_field,$confirmation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment