Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Last active August 29, 2015 14:07
Show Gist options
  • Save vishalbasnet23/e79c845186451ef89e4a to your computer and use it in GitHub Desktop.
Save vishalbasnet23/e79c845186451ef89e4a to your computer and use it in GitHub Desktop.
html-table-row-export-php
<?php function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2020 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");
}
add_action('wp_ajax_export_pdf','export_csv');
function export_csv() {
$table_content = $_POST['all_value'];
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
// create a file pointer connected to the output stream
echo array2csv($table_content);
die;
}
?>
$('.export-csv').click(function(){
var nearestRow = $(this).closest('tr');
// console.log(nearestRow);
var allValue = [];
nearestRow.each(function() {
var eventDate = $(this).find('.event-date').text();
allValue.push(eventDate.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var eventName = $(this).find('.event-name').text();
// console.log(eventName.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
allValue.push(eventName.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var eventLocation = $(this).find('.event-location').text();
allValue.push(eventLocation.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var eventInstructor = $(this).find('.event-instructor').text();
allValue.push(eventInstructor.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var approvedRegistration = $(this).find('.apvd-registration').text();
allValue.push(approvedRegistration.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var pendingRegistration = $(this).find('.pend-registration').text();
allValue.push(pendingRegistration.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var notApprovedRegistration = $(this).find('.noapvd-registration').text();
allValue.push(notApprovedRegistration.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var totalAmount = $(this).find('.total-price').text();
allValue.push(totalAmount.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var totalPaid = $(this).find('.total-paid').text();
allValue.push(totalPaid.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var totalDue = $(this).find('.due-amount').text();
allValue.push(totalDue.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
});
/*ajax call*/
var data = {
'action': 'export_csv',
'all_value': allValue
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
console.log(response);
});
/* ajax call end*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment