Issue in data not printing in exported csv file

#1

basically I am passing the selected values of dropdown menus present in form to the
export function I wrote in controller by using ajax call like this

         $("body").on("click","#download", function(){
                  var f_id = $("#f_id").val();
                  var s_id = $("#s_id").val();
                  var t_id = $("#t_id").val();
      $.ajax({
        async:false,
        url:"<?php echo site_url().'export/export_csv'?>/"+f_id+'/'+s_id+'/'+t_id,
        success: function(response) {
            //
        }
    });
});

after cicking download ,I am getting all the values correctly and also getting the data from mysql database using those values but at the end of function when fputcsv function get called like this

           fputcsv($output, $data);

the file is downloading successfiully ,but In exported file only the headers are getting printed not the data I am getting in $data as an array which is required while exporting csv and print the data in file

This is my export_csv function in controller

   public function export_csv($f_id,$s_id,$t_id){
     $fileName = "Ex.csv";    
     // temporary creates file
    $output = fopen('php://memory', 'w');      
    // for message headers
    $csvHeaders = array(
        '0' => array(
            '0' => 'Colummn 1',
            '1' => 'Column 2',
            
        )
    );
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $fileName . '";');

    foreach ($csvHeaders as $value) {
       fputcsv($output, $value);
       fwrite($output, "\t" . "\r\n");
   }
    $details = $this->my_model->get_details($f_id,$s_id,$t_id);
   foreach ($details as $data) {
       fputcsv($output, $data);
       fwrite($output, "\t" . "\r\n");
   }
 }

How can I fix this , please guide me ?

#2

Do you mean here that you have checked that the data is coming back in the $details array and appearing correctly in your foreach() loop, or are you just describing what you think is happening? That is, have you checked that your data is being returned correctly?

If the array contains data in the format you are expecting it, that would perhaps suggest that something is happening to your $output variable. Could your get_details() function be causing a problem there?

Do you close the output file somewhere? I am not sure whether this makes any difference these days, but just seems “right” to me that you should.

#3

Yes I am getting the data correctly which is looping in foreach loop correctly but the blank data is printing in csv file

#4

Is it writing blank data for each line, or is it only outputting the header lines and nothing after them?

1 Like
#5

hey thaks for reply I have solved that issue , there was an issue while fetching the data now its working now actually I am trying to export data which is in different language other than english but after exporting I am not getting the data in specified language
What should I do to fix it , I am stucked?

#6

I can’t see how anything would know what language your data is in. Or do you mean that you have a problem with the character-encoding or character set?