CSV Export, jumping fields.
-
If I create a form with a select field, the CSV output changes, depending on whether the Checkbox was checked or not.
Example form:
Name: [text* your-name] [checkbox vegetarian exclusive "I'm a vegetarian"] Email: [email* your-email] [submit "Send"]
If I submit the form the first time without checking the box, and the second time when checking the box I get this in the CSV export:
As you can see, the checkbox field and email field are in the wrong spot on the third line of the CSV.
-
Charge your CSV client (Excel or calc) delimiter preference.
Hi Arshid,
Thanks for the reply, but I’m afraid this has nothing to do with this. The Raw CSV result is as folows:
Form_id,Form_date,Status,Name,Email,Vegetarian 3,"2018-01-16 14:00:46",unread,"Jules Colle",[email protected], 4,"2018-01-16 14:01:27",unread,"Veggie Veggerson","I'm a vegetarian",[email protected]
I’m well aware of the inner workings of your plugin and of contact form 7 so please allow me to give you my thoughts on what is going wrong here.
The problem is that when a form gets submitted without any checkboxes checked, the submitted form will not contain this field. However, your plugin assumes that all form fields are submitted, so while looping trough them and adding them to the database, when no checkbox is checked, the checkbox field is omitted. Next time, however, when the form does contain the checked box, a new header is created on the fly and somehow the order gets messed up. I can create more examples like this if you like.
-
This reply was modified 7 years, 1 month ago by
Jules Colle.
So, to further elaborate, this is the data that gets saved to the database
submission 1 (checkbox is not checked)
Form_id => 1 Name => "Jules Colle" Email => [email protected]
submission 2 (checkbox is checked)
Form_id => 2 Name => "Veggie Veggerson" Vegetarian => "I'm a vegetarian" Email => [email protected]
This is all nice and fine. But the problem is that it’s not playing nice with your export function.
I see 2 solutions:
solution 1. Add unchecked checkboxes to the database as well, so subnission 1 would look like this:
submission 1 (checkbox is not checked)
Form_id => 1 Name => "Jules Colle" Vegetarian => false Email => [email protected]
solution 2. Change the way you generate your CSV. So don’t assume that just outputting all fields in order will correspond to the correct header. Here’s a little mockup code to illustrate how I would do this:
$php_array_to_csv = array(); foreach($submission as $line_number => $sub) { foreach ($sub as $field_name => $field_value) { $php_array_to_csv[$field_name][$line_number] = $field_value; } }
then loop again to output the CSV:
foreach($submission as $line_number => $sub) { foreach($php_array_to_csv as $field_name => $line) { echo $submission[$line_number][$field_name].','; // you will probably add some escaping functions here } echo "\n"; }
Please check without ”’ character.
You can contribute in GitHub ??
Hey,
I added a pull request on github.
The export comes out fine now.
Before my code changes I got:
Form_id,Form_date,Status,Name,Vegetarian,Email 1,"2018-01-17 16:20:51",unread,a,"I am a vegetarian",[email protected] 2,"2018-01-17 16:20:57",unread,a,[email protected],
After:
Form_id,Form_date,Status,Name,Vegetarian,Email 1,"2018-01-17 16:20:51",unread,a,"I am a vegetarian",[email protected] 2,"2018-01-17 16:20:57",unread,a,,[email protected]
For anyone interested, here is the full code for inc/export-csv.php
<?php /** * CFDB7 csv */ if (!defined( 'ABSPATH')) exit; class Expoert_CSV{ /** * Download csv file * @param String $filename * @return file */ public function download_send_headers( $filename ) { // disable caching $now = gmdate("D, d M Y H:i:s"); header("Expires: Tue, 03 Jul 2001 06:00:00 GMT"); header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); header("Last-Modified: {$now} GMT"); // force download header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); // disposition / encoding on response body header("Content-Disposition: attachment;filename={$filename}"); header("Content-Transfer-Encoding: binary"); } /** * Convert array to csv format * @param array &$array * @return file csv format */ public function array2csv(array &$array){ if (count($array) == 0) { return null; } ob_start(); $df = fopen("php://output", 'w'); $array_keys = array_keys($array); $heading = array(); $unwanted = array('cfdb7_', 'your-'); foreach ($array_keys as $aKeys) { $tmp = str_replace($unwanted, '', $aKeys); $heading[] = ucfirst($tmp); } fputcsv($df, $heading); foreach ($array['form_id'] as $line => $form_id) { $line_values = array(); foreach($array_keys as $array_key ) { $line_values[$array_key] = $array[$array_key][$line]; } fputcsv($df, $line_values); } fclose($df); return ob_get_clean(); } /** * Download file * @return csv file */ public function download_csv_file(){ global $wpdb; $cfdb = apply_filters( 'cfdb7_database', $wpdb ); $table_name = $cfdb->prefix.'db7_forms'; if( isset($_REQUEST['csv']) && isset( $_REQUEST['nonce'] ) ){ $nonce = $_REQUEST['nonce']; if ( ! wp_verify_nonce( $nonce, 'dnonce')) { wp_die( 'Not Valid.. Download nonce..!! ' ); } $fid = (int)$_REQUEST['fid']; $results = $cfdb->get_results("SELECT form_id, form_value, form_date FROM $table_name WHERE form_post_id = '$fid' ",OBJECT); $data = array(); $i = 0; foreach ($results as $result) : $i++; $data['form_id'][$i] = $result->form_id; $data['form_date'][$i] = $result->form_date; $resultTmp = unserialize( $result->form_value ); $upload_dir = wp_upload_dir(); $cfdb7_dir_url = $upload_dir['baseurl'].'/cfdb7_uploads'; foreach ($resultTmp as $key => $value): if (strpos($key, 'cfdb7_file') !== false ){ $data[$key][$i] = $cfdb7_dir_url.'/'.$value; continue; } if ( is_array($value) ){ $data[$key][$i] = implode(', ', $value); continue; } $data[$key][$i] = str_replace( array('"',''','/','\') , array('"',"'",'/','\\'), $value ); endforeach; endforeach; $this->download_send_headers( "cfdb7-" . date("Y-m-d") . ".csv" ); echo $this->array2csv( $data ); die(); } } }
Hey,
Has this topic been resolved / the above code work?I am facing the same issue as Jules, although I am not familiar with .php files etc.
@jules-colle – I tested your code modifying the php file however it corrupted my wordpress rendering it inaccessible… Below is the error that displayed.
“Parse error: syntax error, unexpected ”,” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in xxxxx/contact-form-cfdb7/inc/export-csv.php on line 104″
what is on line 104? You got a syntax error. at least a basic knowledge of php is needed if you start modifying php code. I added the change to GitHub but it was never implemented ??
-
This reply was modified 6 years, 10 months ago by
Jules Colle.
@jules-colle – basic knowledge needed indeed. I am no dev ??
I saw the github request.
Re 104… I copied and pasted the above (your) code and replaced my original file. Assuming I need to paste it elsewhere in the file rather than replacing the original code?
(or the syntax is in the above code)Fixed in the current version.
Thank youI am testing out CFDB7 1.2.4.5 and have encountered the same problem.
1. I submitted a form in which all fields are entered. The exported CSV is output with columns in the same order as the form fields.
2. I submitted a form in which some check-boxes are null. The exported CSV is output with the columns for null check-boxes following those for all other fields (obviously, this applies to the first form as well).
3. I submitted another form in which all fields are entered. The exported CSV is once again output with all columns in the same order as the form fields.
Conclusion. The order of columns in the CSV depends on whether or not the final record submitted has any null check-boxes. If so, these columns will be moved to the end.
Andy
-
This reply was modified 7 years, 1 month ago by
- The topic ‘CSV Export, jumping fields.’ is closed to new replies.