'How to export from wpdb to csv without including html tags in table?
I'm having trouble setting up an export from my WordPress install to a CSV file. The data displays fine, but there are 8 rows of HTML displaying above it that I cannot get rid of no matter what I do.
Code as follows:
$filename = 'test-' . time() . '.csv';
$header_row = array(
0 => 'field1',
1 => 'field2',
2 => 'field3',
3 => 'field4',
);
$data_rows = array();
global $wpdb, $bp;
$users = $wpdb->get_results( "SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM `TESTTABLE`" );
foreach ( $users as $u ) {
$row = array();
$row[0] = $u->FIELD1;
$row[1] = $u->FIELD2;
$row[2] = $u->FIELD3;
$row[3] = $u->FIELD4;
$data_rows[] = $row;
}
ob_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 );
}
fclose( $fh );
echo "Complete";
die();
?>
Would be much appreciated if I could get a hand with this, as I can't seem to get this data to display without additional stuff up the top!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
