Title: CSV Export, jumping fields.
Last modified: January 16, 2018

---

# CSV Export, jumping fields.

 *  Resolved [Jules Colle](https://wordpress.org/support/users/jules-colle/)
 * (@jules-colle)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/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"]
       ```
   
 * ![](https://i0.wp.com/image.ibb.co/dDZQMm/cf7db_form.png?ssl=1)
 * 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:
 * ![](https://i0.wp.com/image.ibb.co/g2YKgm/cf7db_export.png?ssl=1)
 * As you can see, the checkbox field and email field are in the wrong spot on the
   third line of the CSV.

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Plugin Author [Arshid](https://wordpress.org/support/users/arshidkv12/)
 * (@arshidkv12)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-9870231)
 * Charge your CSV client (Excel or calc) delimiter preference.
 *  Thread Starter [Jules Colle](https://wordpress.org/support/users/jules-colle/)
 * (@jules-colle)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-9870989)
 * 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",jules.colle@gmail.com,
       4,"2018-01-16 14:01:27",unread,"Veggie Veggerson","I'm a vegetarian",vv@imaveggie.com
       ```
   
 * 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 8 years, 4 months ago by [Jules Colle](https://wordpress.org/support/users/jules-colle/).
 *  Thread Starter [Jules Colle](https://wordpress.org/support/users/jules-colle/)
 * (@jules-colle)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-9873270)
 * 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 => jules.colle@gmail.com
       ```
   
 * submission 2 (checkbox is checked)
 *     ```
       Form_id => 2
       Name => "Veggie Veggerson"
       Vegetarian => "I'm a vegetarian"
       Email => vv@imaveggie.com
       ```
   
 * 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 => jules.colle@gmail.com
       ```
   
 * 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";
       }
       ```
   
 *  Plugin Author [Arshid](https://wordpress.org/support/users/arshidkv12/)
 * (@arshidkv12)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-9873505)
 * Please check without ”’ character.
 *  Plugin Author [Arshid](https://wordpress.org/support/users/arshidkv12/)
 * (@arshidkv12)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-9873507)
 * You can contribute in GitHub 🙂
 *  Thread Starter [Jules Colle](https://wordpress.org/support/users/jules-colle/)
 * (@jules-colle)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-9874280)
 * 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",mail@mail.com
       2,"2018-01-17 16:20:57",unread,a,mail@mail.com,
       ```
   
 * After:
 *     ```
       Form_id,Form_date,Status,Name,Vegetarian,Email
       1,"2018-01-17 16:20:51",unread,a,"I am a vegetarian",mail@mail.com
       2,"2018-01-17 16:20:57",unread,a,,mail@mail.com
       ```
   
 * 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('&quot;',''','/','\')
                           , array('"',"'",'/','\\'), $value );
   
                       endforeach;
   
                   endforeach;
   
                   $this->download_send_headers( "cfdb7-" . date("Y-m-d") . ".csv" );
                   echo $this->array2csv( $data );
                   die();
               }
           }
       }
       ```
   
 *  [lteh](https://wordpress.org/support/users/lteh/)
 * (@lteh)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10172202)
 * 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.
 *  [lteh](https://wordpress.org/support/users/lteh/)
 * (@lteh)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10172497)
 * [@jules-colle](https://wordpress.org/support/users/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″
 *  Thread Starter [Jules Colle](https://wordpress.org/support/users/jules-colle/)
 * (@jules-colle)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10172537)
 * 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 8 years, 2 months ago by [Jules Colle](https://wordpress.org/support/users/jules-colle/).
 *  [lteh](https://wordpress.org/support/users/lteh/)
 * (@lteh)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10177248)
 * [@jules-colle](https://wordpress.org/support/users/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)
 *  Plugin Author [Arshid](https://wordpress.org/support/users/arshidkv12/)
 * (@arshidkv12)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10432285)
 * Fixed in the current version.
    Thank you
 *  [andynz](https://wordpress.org/support/users/andynz/)
 * (@andynz)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10993544)
 * I 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

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘CSV Export, jumping fields.’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-cfdb7/assets/icon-256x256.png?rev=1619878)
 * [Database Addon for Contact Form 7 - CFDB7](https://wordpress.org/plugins/contact-form-cfdb7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-cfdb7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-cfdb7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-cfdb7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-cfdb7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-cfdb7/reviews/)

 * 12 replies
 * 4 participants
 * Last reply from: [andynz](https://wordpress.org/support/users/andynz/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/csv-export-jumping-fields/#post-10993544)
 * Status: resolved