Skip to content

Instantly share code, notes, and snippets.

@accessomnath
Last active July 16, 2016 11:36
Show Gist options
  • Save accessomnath/1b50536cbd6dccfcc0d9a78107eee80f to your computer and use it in GitHub Desktop.
Save accessomnath/1b50536cbd6dccfcc0d9a78107eee80f to your computer and use it in GitHub Desktop.
<div id="dvData">
<table id="testbed_results" style="width:70%" border="1px solid black">
<tr>
<th>Date</th>
<th>Cutomer ID</th>
<th>Transaction ID</th>
<th>Item ID Purchased</th>
<th>Basic Credits</th>
<th>Bonus Credits</th>
<?php
global $wpdb;
$count = 0;
$result = $wpdb->get_results("SELECT purchase_date, customer_id, transaction_id, giftcard_id, giftcard_rem_value, giftcard_post_id, extra_credit_rem_value
FROM id_giftcard_details where 1 {$where}", ARRAY_A);
echo '<pre>';
foreach ($result as $print) {
$basic_credit = get_post_meta($print['giftcard_post_id'], 'actual-price', true);
$bonus_credit = get_post_meta($print['giftcard_post_id'], 'additional-credit', true);
echo '<tr>';
echo '<td>' . date('d-m-Y', strtotime($print['purchase_date'])) . '</td>';
echo '<td><a href="' . $link . $print["customer_id"] . '">' . $print['customer_id'] . '</a></td>';
echo '<td>' . $print['transaction_id'] . '</td>';
echo '<td>' . $print['giftcard_id'] . '</td>';
echo '<td>' . $basic_credit . '</td>';
echo '<td>' . $bonus_credit . '</td>';
echo '</tr>';
$count++;
}
?>
</table>
</div>
<span class="">Total :<?php echo $count; ?> </span><br>
<!-- <input type="hidden" value="--><?php //echo $result ?><!--" id="result" name="button"/>-->
<br>
<div class='button'>
<a href="#" id ="export" role='button'>Export Data
</a>
</div>
<!-- <input type="button" data-export="export" value="Export" id="export" class="export" name="button"/>-->
<!-- <input type="button" data-export="export" value="Export" id="btnExport" class="export" name="button"/>-->
<?php
} else {
getCustomerCreditsHistory($user_id);
}
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- <script>-->
<!-- $(function () {-->
<!-- $("#export").click(function () {-->
<!-- $("#export_table").tableToCSV();-->
<!-- });-->
<!-- });-->
<!-- </script>-->
<script type='text/javascript'>
$(document).ready(function () {
console.log("HELLO")
function exportTableToCSV($table, filename) {
var $headers = $table.find('tr:has(th)')
,$rows = $table.find('tr:has(td)')
// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
,tmpColDelim = String.fromCharCode(11) // vertical tab character
,tmpRowDelim = String.fromCharCode(0) // null character
// actual delimiter characters for CSV format
,colDelim = '","'
,rowDelim = '"\r\n"';
// Grab text from table into CSV formatted string
var csv = '"';
csv += formatRows($headers.map(grabRow));
csv += rowDelim;
csv += formatRows($rows.map(grabRow)) + '"';
// Data URI
var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
$(this)
.attr({
'download': filename
,'href': csvData
//,'target' : '_blank' //if you want it to open in a new window
});
//------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------
// Format the output so it has the appropriate delimiters
function formatRows(rows){
return rows.get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim);
}
// Grab and format a row from the table
function grabRow(i,row){
var $row = $(row);
//for some reason $cols = $row.find('td') || $row.find('th') won't work...
var $cols = $row.find('td');
if(!$cols.length) $cols = $row.find('th');
return $cols.map(grabCol)
.get().join(tmpColDelim);
}
// Grab and format a column from the table
function grabCol(j,col){
var $col = $(col),
$text = $col.text();
return $text.replace('"', '""'); // escape double quotes
}
}
// This must be a hyperlink
$("#export").click(function (event) {
// var outputFile = 'export'
var outputFile = window.prompt("What do you want to name your output file (Note: This won't have any effect on Safari)") || 'export';
outputFile = outputFile.replace('.csv','') + '.csv'
// CSV
exportTableToCSV.apply(this, [$('#dvData>table'), outputFile]);
// IF CSV, don't do event.preventDefault() or return false
// We actually need this to be a typical hyperlink
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment