3
votes

I have created a admin menu page in my custom theme's functions.php file and i want to export data in csv format when click on that menu.

I have written following code for that but when i click on that menu i got result in garbage format in excel.

enter image description here

Following is my code written in custom theme's functions.php file

1) add custom admin menu page

<?php
add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
      add_menu_page('Export Payment History', 'Export Payment History', 'manage_options', 'export-history', 'export_payment_history','dashicons-image-rotate-right' );
}
?>

2) call back function

function export_payment_history(){
global $wpdb, $user_id;
$c_user = get_current_user_id();

$filename = "users_csv.csv";
$data_rows = array();


$results = $wpdb->get_results("SELECT * FROM wp_custom_payment");

$header_row = array(
    0 => 'ID',
    1 => 'TRANSACTION ID',
    2 => 'EMAIL',
    3 => 'PAYMENT DATE',
    4 => 'TOTAL AMOUNT',

  );


$i = 1;
foreach( $results as $result ) {

  $item_unserialize = unserialize($result->item_data);

  $user_info = get_userdata($result->user_id);
  $user_email =  $user_info->user_email;

  $row = array();
    $row[0] = $i;
    $row[1] =  $result->txn_id;
    $row[2] = $user_email;
    $row[3] =  $result->payment_date;
    $row[4] = $item_unserialize['payment_total']['payment_gross'];

    $data_rows[] = $row;

  $i++;
}

$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Expires: 0' );
header( 'Pragma: public' );



fputcsv($fh, $header_row);

  foreach ($data_rows as  $data_row) {  
    fputcsv($fh, $data_row);
  }

  fclose( $fh );
  exit();

}

I got garbage value in excel sheet, not getting data anyone please help me.

I got output like following enter image description here

1
whats this line for fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) ); - ArtisticPhoenix
I have also removed that line. - Samir Sheikh
Have you looked at the actual output? I don't know what this means got result in garbage format - ArtisticPhoenix
I have uploaded screen shot of output. - Samir Sheikh
Obviously you are picking up output from wordpress, ie. HTML contents. I would try using php://temp However even that will probably fail, you have to do it without including the wordpress header and such. It's to general for me to guess how your site is setup. - ArtisticPhoenix

1 Answers

0
votes

First of all thank you to all guys , I have resolved it by my self

I have just add stream_get_contents.

Full code..

function export_payment_history(){
global $wpdb, $user_id;
$c_user = get_current_user_id();

$filename = "users_csv.csv";
$data_rows = array();


$results = $wpdb->get_results("SELECT * FROM wp_custom_payment");

$header_row = array(
    0 => 'ID',
    1 => 'TRANSACTION ID',
    2 => 'EMAIL',
    3 => 'PAYMENT DATE',
    4 => 'TOTAL AMOUNT',    
  );


$i = 1;
foreach( $results as $result ) {

  $item_unserialize = unserialize($result->item_data);

  $user_info = get_userdata($result->user_id);
  $user_email =  $user_info->user_email;

  $row = array();
    $row[0] = $i;
    $row[1] =  $result->txn_id;
    $row[2] = $user_email;
    $row[3] =  $result->payment_date;
    $row[4] = $item_unserialize['payment_total']['payment_gross'];

    $data_rows[] = $row;

  $i++;
}

ob_end_clean();
$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Expires: 0' );
header( 'Pragma: public' );

fputcsv($fh, $header_row);

  foreach ($data_rows as  $data_row) {  
    fputcsv($fh, $data_row);
  }

$csvFile = stream_get_contents($fh);
fclose($fh);
die();
}